Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the unix timestamp of midnight on the previous Wednesday

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.

like image 401
Alex S Avatar asked Jul 21 '09 20:07

Alex S


People also ask

How can I get midnight time?

00:00 is midnight at the beginning of a day, 24:00 is midnight at the end of a day. For simplicity however, most digital clocks skip 24:00 - declaring that midnight is the start of a new day. I.e. 8th of Feb 24:00 and 9th of Feb 00:00 is in practice the same point in time.

How Unix timestamp is calculated?

The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.

What is timestamp in Unix format?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC. One of the primary benefits of using Unix time is that it can be represented as an integer making it easier to parse and use across different systems.


1 Answers

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.

like image 159
Pascal MARTIN Avatar answered Sep 18 '22 10:09

Pascal MARTIN