有没有一种简单的方法来计算某个日期时间午夜过后的秒数?不是离现在还有多少秒,那一天过去的几秒
例如:
2015-04-10 00:00:00
应返回0
2015-04-12 09:20:00
应返回33600
2015-04-14 15:20:00
应返回55200
有没有一个很好的短方法来做这件事?
这里有一个简单的代码。
任何一天代码
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime(date("Y-m-d 00:00:00", strtotime($date)));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
当日代码
<?php
$midnight = strtotime("midnight");
$now = date('U');
$diff = $now - $midnight;
echo $diff;
?>
也许更干净的代码是使用strtotime("midnight", <EpochTime>);
<?php
$date = "2015-04-12 09:20:00";
$midnight = strtotime("midnight", strtotime($date));
$now = strtotime($date);
$diff = $now - $midnight;
echo $diff;
?>
一天是24小时是60分钟是60秒是1000毫秒,所以时间戳%(24*60*60*1000)可能会起作用,至少如果您与epoch php使用的时区相同(或者时间戳来自任何地方)。它至少不会对准确性要求太高。PHP使用系统的时间,所以很多事情都会打破这个想法(更改时钟、夏令时、闰秒等)
我个人会使用timestamp-startofday时间戳,并使用该语言的日历进行计算。