Get the unix timestamp of midnight on the previous Wednesday

datetime, php, unix-timestamp

Solution

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.

Problem

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.

Original source