[英]DateTime::diff() isn't affected by timezone but always return same value
I am adding auction-based data to my system. 我正在将基于拍卖的数据添加到我的系统中。 For example:
例如:
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
I store all these information as it is in my DB (ie all stored time based on user's timezone ). 我将所有这些信息直接存储在数据库中(即, 所有存储的时间均基于用户的时区 )。
Now I want the actual difference between end time and current time (of America/Denver timezone). 现在,我想要结束时间与当前时间 (美国/丹佛时区) 之间的实际差异 。
I used DateTime()
functions 我使用了
DateTime()
函数
so even I don't convert the timezone;
所以即使我不转换时区; it returns same difference (and that is wrong too).
它返回相同的差异(这也是错误的)。
My PHP code is below; 我的PHP代码如下; you can also find it at CodePad
您也可以在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);
}
Returns: 返回值:
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
References: 参考文献:
On the following lines you're basically saying that the end date is in GMT while it should be in America/Denver time: 在以下几行中,您基本上是在说结束日期是格林尼治标准时间,而应该是美国/丹佛时间:
$to = new DateTime($end, new DateTimeZone('GMT'));
Then you intent to convert it to America/Denver time but since you created the date using the GMT timezone this would have been wrong anyway. 然后,您打算将其转换为美国/丹佛时间,但是由于使用GMT时区创建了日期,因此无论如何都会出错。
// $to ->setTimeZone($owner_tz);
This will create the end date using the users timezone which is probably what you are looking for: 这将使用用户时区来创建结束日期,这可能是您要查找的内容:
$to = new DateTime($end, $owner_tz);
I got the solution, so i would like to share 我有解决方案,所以我想分享
Actually I have set default timezone in config.inc.php
file as below 实际上我已经在
config.inc.php
文件中设置了默认时区,如下所示
date_default_timezone_set('America/Los_Angeles');
then I check the current time and timezone of MySQL server from phpmyadmin with below query 然后我通过以下查询从phpmyadmin检查MySQL服务器的当前时间和时区
SELECT NOW(), SYSDATE(), @@global.time_zone , @@session.time_zone ,
TIMEDIFF( NOW( ) , CONVERT_TZ( NOW( ) , @@session.time_zone , '+00:00' ))
AS OFFSET
This return the OFFSET
value +05:30 这将返回
OFFSET
值+05:30
First I changed the timezone of mySQL Server to GMT/UTC +00:00 ( I have super privilage on mySQL server) 首先,我将mySQL Server的时区更改为GMT / UTC +00:00 (我在mySQL Server上拥有超级特权)
SET GLOBAL time_zone = '+00:00';
start_date = NOW()
( column datatype: DATETIME
) start_date = NOW()
保存日期和时间(列数据类型: DATETIME
) Now there is 2 way to get date and time as per user's timezone ( America/Denver
) 现在有两种方法可以根据用户的时区(
America/Denver
)获取日期和时间
/*
* first set timezone as GMT.
* This is MUST because default timezone is differ from user timezone
*/
$gmt = new DateTimeZone('GMT');
$user_tz = 'America/Denver';
$st = new DateTime($row[`start_date`], $gmt);
// now set user timezone
$st->setTimezone($user_tz );
$stime = $qt->format('r');
echo $stime;
#$retrieve data from server in timestamp
$qry = "SELECT `start_date`,UNIX_TIMESTAMP(`start_date`) AS sTimestamp FROM..."
$st = new DateTime('@'.$row['sTimestamp ']);
$stime = $st->format('r');
echo $stime;
Note : dont change start_date
to timestamp with strtotime()
. 注意:不要使用
strtotime()
将start_date
更改为时间戳。 It will return different value from the UNIX_TIMESTAMP()
ie 它将从
UNIX_TIMESTAMP()
返回不同的值,即
strtotime($row['start_date']) !== $row['sTimestamp']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.