tags:

views:

264

answers:

2

I know there is a lot of info on the neton how to show yesterdays date but I am unable to get any of them to work in this format

How do you show yesterdays date if todays date is in this format date('F jS, Y') ?

July 27th, 2009 should show July 26th, 2009

//Does not work
$yesterday = date('F jS, Y', mktime(0, 0, 0, date("F") , date("j") - 1, date("Y")));
echo $yesterday;
+3  A: 

Use the very awesome strtotime:

$today = 'July 27th, 2009';
$yesterday = date('F jS, Y', strtotime('yesterday', strtotime($today)));
print $yesterday; // July 26th, 2009
Paolo Bergantino
thanks I'll use the other solution below but it's good to know different ways of doing it +1
jasondavis
+2  A: 

easier: $yesterday = date('F jS, Y', time()-86400);

dusoft
Someone did a big test and found that this was the most efficient method.
Chacha102
great performance is always an issue on my site
jasondavis
I have not tested this, but I have a feeling it will not work properly for at least one hour a year because of daylight savings. I have a hunch that somewhere around the day when an hour is added or the day when an hour is removed, +/- 24 hours. Maybe someone else can look into this a bit deeper, maybe this isn't a real problem for you...
Tom
of course it will work.
dusoft
There is a 23 hour day and a 25 hour day each year when daylight saving time switches. As a result, your code will be one day off two hours a year. It also doesn't account for leap seconds.
alberge