views:

557

answers:

4

How would I go about finding the unix timestamp of midnight of the previous Wednesday? My only approach would be to get the day index and day number of today, and subtract the difference, but I can think of several scenarios where this would fail, for example, early in a month before a Wednesday has happened.

I guess, more concisely, how do I find the date of the previous Wednesday?

Any help would be appreciated.

+1  A: 

It's easier than you might think...observe the awesome 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 output

15 Jul 2009 00:00:00

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

Paul Dixon
+3  A: 

What about strtotime ?

$timestamp = strtotime("last Wednesday");

var_dump($timestamp);
var_dump(date('Y-m-d H:i:s', $timestamp)); // to verify

And you get this output :

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

which is indeed last wednesday, midnight.

Pascal MARTIN
A: 

How "correct" do you want to be? If you want to be highly correct, you'll need to account for daylight savings time, time zones, leap years, etc. This means using a library and not just subtracting day indexes.

I am not currently doing PHP but I used to have good luck with the Pear libraries: here are the docs for the Calendar class

Peter Recore
A: 

excellent ! i like it..good

zahoor