I have a system that compares fiscal year up to the current date to the same date range of the year before. You can adjust the year and month to look at and it always compares the same date range to the year previous. I have it set so if the current day is leap day it compares to the 28th of last year, and if last year was leap year and today is feb 28th it compares to last year up to 29th. if you look at a month other than the current month it shows up to the last day of that month, otherwise up to current date.
OK that works fine right now, but now my employers don't want it to be up to the current date they want it to go up to yesterdays date. How can I do that, my main concerns are what if today is the 1st of the month, or what if today is the first day of the fiscal year.
Here is the code I have now:
function create_YTD_XML[]
{
global $month;
global $year;
$last_year = $year - 1;
if[$year == date['Y'] && $month == date['m']]
{
$this_day = date['d'];
}
else
{
$this_day = date['t', mktime[0, 0, 0, $month, 1, $year]]; // LAST DAY OF MONTH
}
if[is_leap_year[$year] && $this_day == 29]
{
$last_day = 28;
}
else if[is_leap_year[$last_year] && $this_day == 28]
{
$last_day = 29;
}
else
{
$last_day = $this_day;
}
if[$month >= 2]
{
$this_year_start = $year;
$last_year_start = $last_year;
}
else
{
$this_year_start = $year - 1;
$last_year_start = $last_year - 1;
}
$this_ytd_start = $this_year_start.'-02-01';
$last_ytd_start = $last_year_start.'-02-01';
$this_ytd_end = $year.'-'.str_pad[$month, 2, "0", STR_PAD_LEFT].'-'.$this_day;
$last_ytd_end = $last_year.'-'.str_pad[$month, 2, "0", STR_PAD_LEFT].'-'.$last_day;
}
What would be the best solution?
Thanks!
Hi,
Simple method how to get the date of tomorrow, yesterday, etc. is to use:
$tomorrow = date[ "Ymd", strtotime[ "+1 days" ] ];
$dayaftertomorrow = date[ "Ymd", strtotime[ "+2 days" ] ];
$yesterday = date[ "Ymd", strtotime[ "-1 days" ] ];
$daybeforeyesterday = date[ "Ymd", strtotime[ "-2 days" ] ];
The date format can be modified, so you should get what you need.
You can do that quite easy using the PHP function strtotime[]. You can pass a date in text form to this function and you get a timestamp back.
You can even work with time constants such as day, week, month or year. Here are some examples for you:
$t = strtotime["-1 day"]; echo date["d.m.Y", $t];
This outputs the date of yesterday, that is the date one day back in past.
$t = strtotime["-7 days"]; echo date["d.m.Y", $t];
This outputs the date before one week, that is the date before seven days.
$t = strtotime["-10 days"]; echo date["d.m.Y", $t];
Using this code, you get the date ten days ago.
$t = strtotime["-100 days"]; echo date["d.m.Y", $t];
And using this code, you get the date one hundred days ago.
2016-11-01 at 23:52
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Adding days to $Date can be done in many ways. It is very easy to do by using built-in functions like strtotime[], date_add[] in
PHP.
Method 1: Using strtotime[] Function: The strtotime[] Function is used to convert an English textual date-time description to a UNIX timestamp.
Syntax:
strtotime[ $EnglishDateTime, $time_now ]
Parameters: This function accepts two optional parameters as mentioned above and described below.
- $EnglishDateTime: This parameter specifies the English textual date-time description, which represents the date or time to be returned.
- $time_now: This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.
Program: PHP program to add days to $Date in PHP using strtotime[] function.
php
Method 2: Using date_add[] Function: The date_add[] function is used to add days, months, years, hours, minutes and seconds.
Syntax:
date_add[object, interval];
Parameters: This function accepts
two parameters as mentioned above and described below:
- Object: It specifies the DateTime object returned by date_create[] function.
- Interval: It specifies the DateInterval object.
Program: PHP program to add days to $Date in PHP using date_add[] function.