PHP: Convert Local Time to UTC

前端 未结 2 934
忘掉有多难
忘掉有多难 2021-01-22 18:30

Assume I get a string like 08/22/2015 10:56 PM and that this date/time string always refers to only one particular time zone. I need to be able to convert that to t

相关标签:
2条回答
  • 2021-01-22 19:20

    according to subject line; To convert local time to UTC you need the timezone name of given local time, or if you have timezone offset in GMT; you can try the following:

    //assume time zone is +3 hours
    $offset = 3 * 60 * 60;
    $date = date('H:i:s', strtotime("10:56 PM")- $offset);
    echo $date;
    

    This will output : 19:56:00 which is UTC Time

    0 讨论(0)
  • 2021-01-22 19:23

    You can use DateTime, example bellow:

    <?php
    
    $datetime = '08/22/2015 10:56 PM';
    $tz_from = 'America/New_York';
    $tz_to = 'UTC';
    $format = 'Ymd\THis\Z';
    
    $dt = new DateTime($datetime, new DateTimeZone($tz_from));
    $dt->setTimeZone(new DateTimeZone($tz_to));
    echo $dt->format($format) . "\n";
    
    $minutes = 30;
    $dt->add(new DateInterval('PT' . $minutes . 'M'));
    echo $dt->format($format) . "\n";
    
    0 讨论(0)
提交回复
热议问题