Getting Yesterday’s Date in PHP

by Terri Ann on July 2, 2009

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.

{ 0 comments }

Where the Heck Have I Been??

by Terri Ann on February 20, 2009

Where have I been you ask? Well moving, working shooting and coding. Every now and then I sleep too.

Photography Gigs Through Model Mayhem

Lately I’ve been booking as many TF* shoots with models through model mayhem you can see the results on my Flickr account

Model Mayhem Shots

Also I’ve been trying to clean up this mess of a blog – and I’ve crossed into production with Phase A: launch new alternate site.

New Site Launched: Learn More Photo.com

LearnMorePhoto.com went live today with a great new post 6 Golden Rules for Photographers Working With Models

This site is targeted towards everyone who reads my blog for photography related posts. Now you’ll be able to find everything all near clean and organized into a photographic centric site.

If you want to subscribe to that RSS feed it’s at: http://feeds2.feedburner.com/LearnMorePhotography


What’s Next for Ninedays and Terri?

What’s next, you ask? Yes, all my code and WordPress related posts will be moved to it’s own independent site as well. Then all my other content will port to a new home of a more ‘personal’ nature.


If you have any suggestions or tips or requests during this time of change drop me a comment and let me know what I’m doing wrong or right by you!

{ 0 comments }

Reviewing Ninedays Blog – Traffic Questions

January 2, 2009

Part two of a year end review inspired by pro blogger.

Read the full article →

Reviewing Ninedays Blog – General Questions

December 31, 2008

Part one of a year end review inspired by pro blogger.

Read the full article →

Free iPhone, Storm or G1 from Chitika

December 15, 2008

Chitika wants you to win a new fancy phone- iPhone, Android or Storm – take your pick. Just run Chitika with AdSense creatively and enter the contest!

Read the full article →

Clipboard Express – Wordpress Plugin – New Release!

December 11, 2008

Two weeks ago I began using the WordPress Plugin SVN repo to create a WP plugin for work. I also registered one of my favorite plugins as well: Clipboard Express.

Over the past few days I’ve finalized the 1.2 release of Clipboard Express in reparation to add it to the WordPress Plugin directory. It’s [...]

Read the full article →

7 Ways to Find an Up and Coming Bloggers

December 4, 2008

Seven suggestions and tools to help you find the next big thing in blogging for your niche. It’s important know who the big mouths in your niche are and to make contacts and relationships with other bloggers early on so that you can build your readership and network of personal contacts. You want to be able to say you knew them [the big mouths] when…

Read the full article →

Has My Absence Made Your Heart Grow Fonder?

December 4, 2008

Busy with the noew job, learning and getting a new site set up for all my top posts from the Ninedays Blog. Are you ready? 2009 is bound to be my biggest year ever.

Read the full article →

15 Creative Ways to Display Date Archives

October 26, 2008

Date based archives aren’t necessarily the most important thing for your site but if it’s another opportunity to present your information in a different (hopefully creative way) then why not do it? Let’s examine 15 different examples to see what works well, and what lacks in workingness.

Read the full article →

One year Minted

October 3, 2008

One year of Mint stats and approximately 4830% more hits/month since last year, there is no spilled milk to cry over, that’s for sure.

Read the full article →