Getting Yesterday's Date in PHP

It’s a blessing and a curse the way there are so many different methods to calculate things.

For instance calculating or retrieving yesterday’s date in PHP: I came up with 4 quick ways to calculate it.

I always remember method 1, using mktime() but decided to benchmark each of them to see which method of calculation is the most efficient.

I used date("Y-m-d") as a control in all the benchmarks since each of the yesterday calculations are using that function to display.

Note: I may not display my data in the standard programming benchmark styles but that’s because of the way I approach information. I would rather tell you what I learned from the data not just spit out the data and make you do all the thinking. Do you want hard data? Run your own tests. This is about knowledge – not data.

Method 1 – mktime()

echo date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));

This method was by far the slowest. Performing at 450% the time of the control.

Method 2 – Subtracting minutes from the current unix timestamp time()

echo date("Y-m-d", time() - 86400);
// I did not benchmark the following but this is how I would typically use this method:
echo date("Y-m-d", time() - (60*60*24) );

In the benchmark this was the fastest, performing at 103% of the control. Subtracting 86,400, ie. the number of minutes in a day, is accurate under normal circumstances though getting hours or any time during daylight savings time could get a little dicey. As always it depends on your needs.

Method 3 – strtotime() yesterday

echo date("Y-m-d", strtotime("yesterday"));

This is fast but what happens if you need to create a script where you may need to reuse yesterday to calculate the day before yesterday – there is no easy solution with this method, whereas the other 3 methods can either be added to, subtracted from or multiplied to calculate yesterday’s yesterday.

Method 4 – strtotime() -1 day

echo date("Y-m-d", strtotime("-1 day"));

I really like this method, though it’s not the fastest, performing at 203% the time of the control, it’s easily expandable and human readable, just change the 1 to another number or change the – to a + for tomorrow. Yes you can accomplish the same thing multiplying the 86400 in method 2 but it all depends on your style. I can’t easily skim a script and recognize 86400 as yesterday, so often I will use (60*60*24) instead which is as readable as -1 day for me.


As always- run your tests – see if one method is faster for you based on the conditions in your actual script or use what makes more sense to your scripts needs.

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 10 comments }

1 John Milmine July 7, 2009 at 9:32 pm

What about:

$date = new DateTime();
$date->modify('-1 day');
2 Micox April 13, 2010 at 9:46 am

Thanks,
the best review about yesterday in php

3 Kyle Wild April 13, 2010 at 4:30 pm

I tend to use strtotime('yesterday')

4 Marius July 1, 2010 at 10:03 am

What if you would call function date just once?

$now = date('Y-m-d');
list($year, $month, $day) = explode('-',  $now);
echo date("Y-m-d", mktime(0, 0, 0, $month,$day-1,$year));

would it be faster or slower than method 1?

5 David September 20, 2010 at 7:02 am

Hi, I must say I’ve found your blog with good code examples of PHP. It is good for beginers to start PHP language.

6 camden_kid November 3, 2011 at 7:06 am

Very cool.

Thanks.

7 Tim November 23, 2011 at 7:34 am

Hi,

just wanted to add that there’s a mistake in your code! Think about 01.01.2012 and doing this: echo date("Y-m-d", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));

I don’t this this’ll work!
I did it this way:

$yesterday = time()-(60<em>60</em>24);
echo date("Y-m-d", mktime(0, 0, 0,date("m",$yesterday),date("d",$yesterday),date("Y",$yesterday)));

But it’s inspired by this post, so thank you very much ;)

8 Terri Ann
Twitter:
November 30, 2011 at 6:16 pm

Thanks for the response Tim. Since I was mostly exploring the different options I didn’t spend a lot of time to see if troublesome dates like the first of the month, March 1st of a leap year and the first of the year, caused any problems. Your example is a bit convoluted though, why take the time stamp and convert it into integers for the day, month and year and make a time stamp from that when you can instead pass the $yesterday variable like echo date("Y-m-d", $yesterday); like in the second example :)

9 Terri Ann
Twitter:
November 30, 2011 at 6:23 pm

I tend to avoid the DateTime object because I develop locally with Apache installed on my Windows machine and there are known bugs with DateTime on Windows and the last time I tried the bugs still caused problems for me. But great tip for someone whose environments don’t have problems!

10 Danny January 13, 2012 at 2:17 pm

Very nice. Appreciate the performance testing. Am also in favor of avoiding DateTime object.

Comments on this entry are closed.

Previous post:

Next post: