Get the unix timestamp at midnight last Wednesday

How can I find the unix timestamp at midnight last Wednesday? My only approach would be to get the daily index and today's day number and subtract the difference, but I can think of several scenarios where this might fail, for example, a month before Wednesday happened.

I think more briefly, how do I find the date of the previous Wednesday?

Any help would be appreciated.

+6
php datetime unix-timestamp
source share
3 answers

What about strtotime ?

 $timestamp = strtotime("last Wednesday"); var_dump($timestamp); var_dump(date('Ymd H:i:s', $timestamp)); // to verify 

And you get this output:

 int 1247608800 string '2009-07-15 00:00:00' (length=19) 

which is really the last Wednesday, midnight.

+5
source share

It's easier than you might think ... to watch the amazing firepower of this fully operational battle station er, I mean strtotime

 $t=strtotime("last wednesday"); echo strftime("%d %b %Y %H:%M:%S", $t); 

This will lead to the conclusion

 15 Jul 2009 00:00:00 

Which, at the time of writing, is the last time :)

+4
source share

How "right" do you want to be? If you want to be very faithful, you will need to consider daylight saving time, time zones, leap years, etc. This means using a library, not just subtracting daily indices.

I currently don't deal with PHP, but I got really lucky with the Pear libraries: here are the docs for the Calendar class

0
source share

All Articles