DateTime :: diff()不受时区影响,但始终返回相同的值 [英] DateTime::diff() isn't affected by timezone but always return same value

查看:294
本文介绍了DateTime :: diff()不受时区影响,但始终返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的系统添加基于拍卖的数据。例如:

 用户的时区是:`美国/丹佛`
拍卖开始时间是:星期四,2012年6月21日03:46:22 AM
拍卖期:1天。
拍卖结束时间:Fri,22 Jun 2012 03:46:22 AM

我存储所有这些信息,因为它在我的数据库(即,所有存储时间基于用户的时区)。



现在我想要实际的结束时间和当前时间之间的差异(美国/丹佛时区)。



我使用了 DateTime() 功能


所以即使我不转换时区;它返回相同的差异(也是错误的)。


我的PHP代码在下面;您还可以在 CodePad 找到它

  $ end_time ='Fri,22 Jun 2012 03:46:22 AM'; 
$ tz ='美国/丹佛';
dateDifference($ end_time,$ tz);

函数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'));
/ * @注意:我已经评论过setTimeZone(),因为我已经存储
*`end time`根据用户的时区,所以我不需要再次转换
*到用户的时区。您可以在线下方发表评论。
*它不会影响结果。
* /
// $ to - > setTimeZone($ owner_tz);

//使用date_diff()的程序样式
$ interval = date_diff($ from,$ to);
var_dump($ interval);

//使用dateTime :: diff()的OO样式
$ 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

参考文献:




解决方案

我有解决方案,所以我想分享



实际上,我已经在 config.inc.php 文件中设置默认时区,如下



date_default_timezone_set('America / Los_Angeles');



然后我查看当前时间和时间来自phpmyadmin的MySQL服务器区域

  SELECT NOW(),SYSDATE(),@@ global。 time_zone,@@ session.time_zone,
TIMEDIFF(NOW(),CONVERT_TZ(NOW(),@@ session.time_zone,'+00:00'))
AS OFFSET

返回 OFFSET +05:30



解决方案步骤:




  • 首先我更改了时区的mySQL服务器到格林尼治标准时间UTC / UTC +00:00 (MySQL服务器上的超级权限)

      SET GLOBAL time_zone ='+00:00'; 


  • 我们使用 start_date = NOW()(列数据类型: DATETIME



现在有2第一种方法(使用PHP America / Denver



href =http://php.net/manual/en/book.datetime.php =nofollow> DateTime )



  / * 
*首先将时区设置为GMT。
*这是必须的,因为默认时区不同于用户时区
* /
$ gmt = new DateTimeZone('GMT');
$ user_tz ='美国/丹佛';
$ st = new DateTime($ row [`start_date`],$ gmt);
//现在设置用户时区
$ st-> setTimezone($ user_tz);
$ stime = $ qt-> format('r');
echo $ stime;



第二种方法(使用MySQL UNIX_TIMESTAMP



 #$从时间戳中从服务器检索数据
$ qry =SELECT`start_date`,UNIX_TIMESTAMP(`start_date`)AS sTimestamp FROM ...
$ st = new DateTime ('@'。$ row ['sTimestamp']);
$ stime = $ st-> format('r');
echo $ stime;

注意:不要更改 start_date strtotime()为时间戳。它将从返回不同的值UNIX_TIMESTAMP()
ie

  strtotime($ row ['start_date'])!== $ row ['sTimestamp'] 


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 (i.e. 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

so even I don't convert the timezone; it returns same difference (and that is wrong too).

My PHP code is below; you can also find it at 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:

解决方案

I got the solution, so i would like to share

Actually I have set default timezone in config.inc.php file as below

date_default_timezone_set('America/Los_Angeles');

then I check the current time and timezone of MySQL server from phpmyadmin with below query

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

solution steps:

  • First I changed the timezone of mySQL Server to GMT/UTC +00:00 ( I have super privilage on mySQL server)

    SET GLOBAL time_zone = '+00:00';  
    

  • We save the date and time using start_date = NOW() ( column datatype: DATETIME )

Now there is 2 way to get date and time as per user's timezone (America/Denver)

first method ( using PHP DateTime)

 /*
   * 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;

second method (using MySQL UNIX_TIMESTAMP)

#$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(). It will return different value from the UNIX_TIMESTAMP() i.e.

 strtotime($row['start_date']) !== $row['sTimestamp']

这篇关于DateTime :: diff()不受时区影响,但始终返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆