[英]Difference between php timestamp? Or mysql timestamp?
I have two timestamps recorded on a mysql table using php. 我使用php在mysql表上记录了两个时间戳。 How can I calculate the difference between these timestamps in hours using php or mysql? 如何使用php或mysql计算这些时间戳之间的小时数差异?
在php中,您可以只使用常规数学,然后使用date()函数创建以小时表示的日期时间
用PHP完成,请参见: http : //php.about.com/od/advancedphp/qt/math_time_php.htm
If you actual hours (3600 seconds), it's just a matter of subtracting the timestamps and dividing by 3600: 如果您实际使用小时数(3600秒),则只需减去时间戳并除以3600:
$hours = ($timestamp2 - $timestamp1)/3600;
$hours = floor($hours); // to round down.
or similar in SQL. 或类似的SQL。
If you want the "logical hours": 如果要“逻辑小时”:
$tz = new DateTimezone("Europe/Lisbon"); //replace with actual timezone
$d2 = new DateTime("@$timestamp2");
$d2->setTimezone($tz);
$d1 = new DateTime("@$timestamp1");
$d1->setTimezone($tz);
$hours = $d2->diff($d1)->h;
This makes difference in case there has been a DST change between the two times. 如果两次之间的DST发生变化,这将有所不同。 Example: 例:
<?php
$ltz = new DateTimezone("Europe/Lisbon");
date_default_timezone_set("Europe/Lisbon");
$ts2 = strtotime("31-Oct-2010 02:30:00");
$ts1 = strtotime("31-Oct-2010 00:30:00");
$d2 = new DateTime("@$ts2");
$d2->setTimezone($ltz);
$d1 = new DateTime("@$ts1");
$d1->setTimezone($ltz);
var_dump(floor($ts2-$ts1)/3600);
var_dump($d2->diff($d1)->h);
gives: 给出:
float(3) int(2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.