php convert datetime to UTC

后端 未结 16 874
無奈伤痛
無奈伤痛 2020-11-27 12:30

I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.

相关标签:
16条回答
  • 2020-11-27 12:47

    If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.

    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";
    

    This will print this:

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

    And as you can see it takes care of the daylight savings time problem as well.

    A little strange way to solve it.... :)

    0 讨论(0)
  • 2020-11-27 12:47

    alternatively you can try this:

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

    this will output :

    2017-10-25 17:13:20 Asia/Singapore

    you can use this inside the value attribute of a text input box if you only want to display a read-only date.

    remove the 'e' if you do not wish to show your region/country.

    0 讨论(0)
  • 2020-11-27 12:50

    As an improvement on Phill Pafford's answer (I did not understand his 'Y-d-mTG:i:sz' and he suggested to revert timezone). So I propose this (I complicated by changing the HMTL format in plain/text...):

    <?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"; 
    ?>
    
    0 讨论(0)
  • 2020-11-27 12:52

    If you don't mind using PHP's DateTime class, which has been available since PHP 5.2.0, then there are several scenarios that might fit your situation:

    1. If you have a $givenDt DateTime object that you want to convert to UTC then this will convert it to UTC:

      $givenDt->setTimezone(new DateTimeZone('UTC'));
      
    2. If you need the original $givenDt later, you might alternatively want to clone the given DateTime object before conversion of the cloned object:

      $utcDt = clone $givenDt;
      $utcDt->setTimezone(new DateTimeZone('UTC'));
      
    3. If you only have a datetime string, e.g. $givenStr = '2018-12-17 10:47:12', then you first create a datetime object, and then convert it. Note this assumes that $givenStr is in PHP's configured timezone.

      $utcDt = (new DateTime($givenStr))->setTimezone(new DateTimeZone('UTC'));
      
    4. If the given datetime string is in some timezone different from the one in your PHP configuration, then create the datetime object by supplying the correct timezone (see the list of timezones PHP supports). In this example we assume the local timezone in Amsterdam:

      $givenDt = new DateTime($givenStr, new DateTimeZone('Europe/Amsterdam'));
      $givenDt->setTimezone(new DateTimeZone('UTC'));
      
    0 讨论(0)
  • 2020-11-27 12:55

    Try the getTimezone and setTimezone, see the example

    (But this does use a Class)

    UPDATE:

    Without any classes you could try something like this:

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

    NOTE: You might need to set the timezone back to the original as well

    0 讨论(0)
  • 2020-11-27 12:55

    I sometime use this method:

    // 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"));
    

    Works all MOST of the time.

    0 讨论(0)
提交回复
热议问题