首页 文章

php将datetime转换为UTC

提问于
浏览
75

我需要一种简单的方法将日期时间戳转换为UTC(来自服务器所在的任何时区)HOPEFULLY,而不使用任何库 .

15 回答

  • 4

    由于strtotime需要特定的输入格式,因此可以使用DateTime::createFromFormatphp 5.3+ is required

    // set timezone to user timezone
    date_default_timezone_set($str_user_timezone);
    
    // create date object using any given format
    $date = DateTime::createFromFormat($str_user_dateformat, $str_user_datetime);
    
    // convert given datetime to safe format for strtotime
    $str_user_datetime = $date->format('Y-m-d H:i:s');
    
    // convert to UTC
    $str_UTC_datetime = gmdate($str_server_dateformat, strtotime($str_user_datetime));
    
    // return timezone to server default
    date_default_timezone_set($str_server_timezone);
    
  • 0

    使用PHP 5或更高版本,您可以使用 datetime::format 函数(参见文档http://us.php.net/manual/en/datetime.format.php

    echo strftime( '%e %B %Y' , 
        date_create_from_format('Y-d-m G:i:s', '2012-04-05 11:55:21')->format('U')
        );  // 4 May 2012
    
  • 1

    http://php.net/manual/en/function.strtotime.php或者如果您不需要使用字符串而不是时间组件,那么http://us.php.net/manual/en/function.mktime.php

  • 96

    将本地时区字符串转换为UTC字符串 .
    例如新西兰时区

    $datetime = "2016-02-01 00:00:01";
    $given = new DateTime($datetime, new DateTimeZone("Pacific/Auckland"));
    $given->setTimezone(new DateTimeZone("UTC"));
    $output = $given->format("Y-m-d H:i:s"); 
    echo ($output);
    
    • NZDT:UTC 13:00
      if $ datetime = "2016-02-01 00:00:01",$ output = "2016-01-31 11:00:01";
      if $ datetime = "2016-02-29 23:59:59",$ output = "2016-02-29 10:59:59";

    • NZST:UTC 12:00
      if $ datetime = "2016-05-01 00:00:01",$ output = "2016-04-30 12:00:01";
      if $ datetime = "2016-05-31 23:59:59",$ output = "2016-05-31 11:59:59";

    https://en.wikipedia.org/wiki/Time_in_New_Zealand

  • 0

    通用规范化功能,用于格式化从任何时区到其他时区的任何时间戳 . 对于存储关系数据库中不同时区的用户的datetimestamp非常有用 . 对于数据库比较,将时间戳存储为UTC并与 gmdate('Y-m-d H:i:s') 一起使用

    /**
     * Convert Datetime from any given olsonzone to other.
     * @return datetime in user specified format
     */
    
    function datetimeconv($datetime, $from, $to)
    {
        try {
            if ($from['localeFormat'] != 'Y-m-d H:i:s') {
                $datetime = DateTime::createFromFormat($from['localeFormat'], $datetime)->format('Y-m-d H:i:s');
            }
            $datetime = new DateTime($datetime, new DateTimeZone($from['olsonZone']));
            $datetime->setTimeZone(new DateTimeZone($to['olsonZone']));
            return $datetime->format($to['localeFormat']);
        } catch (\Exception $e) {
            return null;
        }
    }
    

    Usage:

    $ from = ['localeFormat'=>“d / m / Y H:i A”,'olsonZone'=>'Asia / Calcutta'];

    $ to = ['localeFormat'=>“Y-m-d H:i:s”,'olsonZone'=>'UTC'];

    datetimeconv(“14/05/1986 10:45 PM”,$ from,$ to); //返回“1986-05-14 17:15:00”

  • 2

    这样做:

    gmdate('Y-m-d H:i:s', $timestamp)
    

    或简单地说

    gmdate('Y-m-d H:i:s')
    

    在UTC中获得“NOW” .

    检查参考:

    http://www.php.net/manual/en/function.gmdate.php

  • 1

    尝试

    回音日期('F d Y',strtotime('2010-01-19 00:00:00'));

    将输出:

    2010年1月19日

    您应该更改格式时间以查看其他输出

  • 0

    使用DateTime:

    $given = new DateTime("2014-12-12 14:18:00");
    echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
    
    $given->setTimezone(new DateTimeZone("UTC"));
    echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC
    
  • 11

    使用strtotime从给定字符串生成时间戳(解释为本地时间)并使用gmdate将其作为格式化的UTC日期返回 .

    示例

    根据要求,这是一个简单的例子:

    echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));
    
  • 19

    作为Phill Pafford's answer的一个改进(我不明白他的'Y-d-mTG:i:sz',他建议恢复时区) . 所以我建议这个(我通过改变普通/文本中的HMTL格式来复杂化):

    <?php
    header('content-type: text/plain;');
    $my_timestamp = strtotime("2010-01-19 00:00:00");
    
    // stores timezone
    $my_timezone = date_default_timezone_get();
    echo date(DATE_ATOM, $my_timestamp)."\t ($my_timezone date)\n";
    
    // changes timezone
    date_default_timezone_set("UTC");
    echo date("Y-m-d\TH:i:s\Z", $my_timestamp)."\t\t (ISO8601 UTC date)\n";
    echo date("Y-m-d H:i:s", $my_timestamp)."\t\t (your UTC date)\n";
    
    // reverts change
    date_default_timezone_set($my_timezone);
    echo date(DATE_ATOM, $my_timestamp)."\t ($my_timezone date is back)\n"; 
    ?>
    
  • 4

    我有时会使用这种方法:

    // It is not importnat what timezone your system is set to.
    // Get the UTC offset in seconds:
    $offset = date("Z");
    
    // Then subtract if from your original timestamp:
    $utc_time = date("Y-m-d H:i:s", strtotime($original_time." -".$offset." Seconds"));
    

    全部工作 MOST .

  • 0

    试试getTimezone and setTimezone,看看例子

    (但这确实使用了一个类)

    更新:

    没有任何课程你可以尝试这样的事情:

    $the_date = strtotime("2010-01-19 00:00:00");
    echo(date_default_timezone_get() . "
    "); echo(date("Y-d-mTG:i:sz",$the_date) . "
    "); echo(date_default_timezone_set("UTC") . "
    "); echo(date("Y-d-mTG:i:sz", $the_date) . "
    ");

    注意:您可能还需要将时区设置回原始时区

  • 0

    或者你可以尝试这个:

    <?php echo (new DateTime("now", new DateTimeZone('Asia/Singapore')))->format("Y-m-d H:i:s e"); ?>
    

    这将输出:

    2017-10-25 17:13:20亚洲/新加坡

    如果您只想显示只读日期,可以在文本输入框的value属性中使用它 .

    如果您不想显示您所在的地区/国家,请删除“e” .

  • 53

    按照以下步骤在用户的本地系统中设置 UTC time any timezone (这将是Web应用程序将不同时区保存为UTC所必需的):

    • Javascript(客户端):
    var dateVar = new Date();
    var offset = dateVar.getTimezoneOffset();
    //getTimezoneOffset - returns the timezone difference between UTC and Local Time
    document.cookie = "offset="+offset;
    
    • Php(服务器端):
    public function convert_utc_time($date)
    {
        $time_difference = isset($_COOKIE['offset'])?$_COOKIE['offset']:'';
        if($time_difference != ''){
            $time = strtotime($date);
            $time = $time + ($time_difference*60); //minutes * 60 seconds
            $date = date("Y-m-d H:i:s", $time);
        } //on failure of js, default timezone is set as UTC below
        return $date;
    }
    ..
    ..
    //in my function
    $timezone = 'UTC';
    $date = $this->convert_utc_time($post_date); //$post_date('Y-m-d H:i:s')
    echo strtotime($date. ' '. $timezone)
    
  • 30

    如果您的日期格式为YYYY-MM-HH dd:mm:ss,您实际上可以通过在“日期时间字符串”末尾添加UTC来欺骗php并使用strtotime进行转换 .

    date_default_timezone_set('Europe/Stockholm');
    print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
    print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";
    

    这将打印出来:

    2009-01-01 13:00:00
    2009-06-01 14:00:00
    

    正如您所看到的那样,它也会解决夏令时问题 .

    有点奇怪的解决方法.... :)

相关问题