Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get 00:00:00 of a unix timestamp

Tags:

html

php

mysql

If I got a unix time which is e.g

1407050129

How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.

Example if i want get today first minute

$today_first_min = strtotime("00:00:00");
like image 655
user3504335 Avatar asked Feb 04 '26 05:02

user3504335


2 Answers

Try this:

$that_day = "1407050129";

$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');

See demo

like image 56
Mark Miller Avatar answered Feb 05 '26 21:02

Mark Miller


An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.

Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!

date_default_timezone_set('UTC');

$that_date  = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));

print date('Y-m-d H:i:s', strval($first_hour));  

// 2014-08-03 00:00:00
like image 25
msturdy Avatar answered Feb 05 '26 22:02

msturdy