我正在将基于拍卖的数据添加到我的系统中。例如:
user's timezone is : `America/Denver`
auction start time is : Thu, 21 Jun 2012 03:46:22 AM
auction period : 1 day.
auction end time : Fri, 22 Jun 2012 03:46:22 AM
我将所有这些信息按原样存储在我的数据库中(即所有存储时间都基于用户的时区)。
现在我想要结束时间和当前时间(美国/丹佛时区) 之间的实际差异。
我使用DateTime()
了函数
所以即使我不转换时区;它返回相同的差异(这也是错误的)。
我的PHP代码如下;您也可以在CodePad上找到它
$end_time = 'Fri, 22 Jun 2012 03:46:22 AM';
$tz = 'America/Denver';
dateDifference($end_time,$tz);
function dateDifference($end, $tz = NULL)
{
$owner_tz = new DateTimeZone($tz);
//var_dump($owner_tz->getName());
$from = new DateTime("now", new DateTimeZone('GMT'));
$from ->setTimeZone($owner_tz);
$to = new DateTime($end,new DateTimeZone('GMT'));
/* @Note:I have commented below setTimeZone() because I have already stored
* `end time` as per user's timezone, So I dont need to convert it again
* into user's timezone.you can un-comment below line.
* it doesn't affect the results anyways.
*/
//$to ->setTimeZone($owner_tz);
//procedural style using date_diff()
$interval = date_diff($from, $to);
var_dump($interval);
// OO style using dateTime::diff()
$interval = $from->diff($to);
var_dump($interval);
}
回报:
object(DateInterval)[4]
public 'y' => int 0
public 'm' => int 0
public 'd' => int 0
public 'h' => int 16
public 'i' => int 40
public 's' => int 24
public 'invert' => int 0
public 'days' => int 6015
参考: