2013-10-31 172 views
0

我有两个时间戳几个小时和几天之间的差距。 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 
+0

检查[DATETIME](HTTP:// PHP。 net/manual/en/class.datetime.php)PHP的对象,或者如果它们是MySQL时间戳,请检查MySQL的DATE_函数以处理日期计算。但是对于我来说,它不是完全清楚你从哪里得到14h和11h ...... – TiMESPLiNTER

+0

时间戳1是开始。这一天(30日)有14个小时,直到午夜。最后是时间戳2.从上午0点到11点11个小时。 – Thomas1703

回答

0

对日期执行操作的最简单方法是将它们转换为时间戳。

在这里,你可以做的是获得三种时间戳:

$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 
0

它在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')))