简体   繁体   English

PHP获取unix时间戳的00:00:00

[英]PHP get 00:00:00 of a unix timestamp

If I got a unix time which is eg 如果我得到一个unix时间,例如

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. 如何在unix时间戳中获取该unix日的12:00 AM,表示该unix时间的第一分钟。

Example if i want get today first minute 如果我想今天第一分钟得到的例子

$today_first_min = strtotime("00:00:00");

Try this: 试试这个:

$that_day = "1407050129";

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

See demo 见演示

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. 另一种获得相同结果的方法... Unix时间是自1970-01-01 00:00:00 UTC以来的秒数,因此每一天都是(60 * 60 * 24)秒的倍数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM