How to find out the date of the day before the date?

I have a system that compares the fiscal year and current date with the same date range of the year. You can set the year and month to view, and it always compares the same date range with the year previous. I set it like this, if the current day is a leap day, it compares with the 28th last year, and if last year was a leap year, and today it is 28th, it compares with last year until the 29th. if you look at a month other than the current month, it is displayed until the last day of that month, otherwise until the current date.

It’s good that it’s working fine now, but now my employers don’t want it to be before the current date, when they want it to return to the date of yesterday. How can I do this, my main problems are whether today is the 1st day of the month, or what if today is the first day of a fiscal year.

Here is the code that 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!

+4
source share
9 answers

strtotime () will do the trick. Convert the previous date to a Unix timestamp using mktime () , then use it like this:

 $from_unix_time = mktime(0, 0, 0, $month, $day, $year); $day_before = strtotime("yesterday", $from_unix_time); $formatted = date('Ym-d', $day_before); 
+23
source

You can also use strtotime using the following words:

 $date = '2012-11-08'; $day_before = date( 'Ym-d', strtotime( $date . ' -1 day' ) ); 

The output of $day_before :

 2012-11-07 
+15
source

You can rely on the ability of mktime () to work with "wrong" values. Suppose $ year, $ month and $ day are the current day.

 $yesterday = mktime (0, 0, 0, $month, $day - 1, $year); echo date ('Ym-d', $yesterday); 

Don’t worry, he will do the right thing: try yourself with different meanings.

+6
source

Note. . The following answer does not include daylight saving time; at certain times of the year he will return an inaccurate result. See Comments for more information.


Since the length of the day is constant (86,400 seconds), you can use arithmetic to determine the timestamp one day before a given date:

 $yesterday = date('Ym-d', strtotime($current_date) - 86400); 

$current_date is the date you want to find the day before. If this is already unix timestamp , you can completely abandon strtotime() :

 $yesterday = date('Ym-d', $current_date_as_unix_timestamp - 86400); 
+3
source
 $date = date('Ym-d',strtotime('yesterday')); 
+2
source

I would use strtotime like this:

 $date="2014-02-20"; echo date("Ymd",strtotime($date." -1 day")); 

will output:

 2014-02-19 
+2
source

I think it depends on the behavior your employer wants. There is no “right” answer - if the fiscal year begins today, do they want to compare the last two fiscal years (what would happen if you used yesterday as the “before” date) or do they want to see day 1 of this fiscal year compared to the first day Last one? You need to sit down and find out possible extreme cases, and then talk with your employer about what they want.

0
source

You can also use the following code:

 $current_date = strtotime('25-11-2012'); echo $yesterday = date('Ym-d', strtotime('-1 day', $current_date)); 

And the code output is as follows:

 2012-11-24 
0
source

One day before the time when fixed 07:59:59

 $demo = date("Ymd H:i:s", mktime(7, 59, 59, date("m"),date("d")-1,date("Y"))); 
0
source

All Articles