我有两个时间戳,间隔在几小时到几天之间。PHP 和/或 MySQL 中有没有办法获取每天的时间片?
INPUT
Timestamp 1: 2013-10-30 10:00:00
Timestamp 2: 2013-10-31 11:00:00
OUTPUT
2013-10-30: 14 hours
2013-10-31: 11 hours
对日期执行操作的最简单方法是将它们转换为时间戳。
在这里,您可以做的是获取三个时间戳:
$ts1 = strtotime("2013-10-30 10:00:00");
$ts2 = strtotime("2013-10-31 00:00:00");
$ts3 = strtotime("2013-10-31 11:00:00");
$first_time_slice = ts2 - ts1; //here you got your first time slice in seconds, convert it to hours
$second_time_slice = ts3 - ts2; //idem for the second time slice
可以在 MySql 中这样完成:
SELECT TIMEDIFF(TIMESTAMP(DATE('2013-10-30 10:00:00') + INTERVAL 1 DAY), '2013-10-30 10:00:00'),
TIMEDIFF('2013-10-31 11:00:00', TIMESTAMP(DATE('2013-10-31 11:00:00')))