Timezone conversion in php

后端 未结 9 1694
我寻月下人不归
我寻月下人不归 2020-11-22 04:19

Can anyone suggest an easy method to convert date and time to different timezones in php?

相关标签:
9条回答
  • 2020-11-22 04:21

    DateTime::setTimezone -- date_timezone_set — Sets the time zone for the DateTime object

    Object oriented style

    <?php
    $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
    echo $date->format('Y-m-d H:i:sP') . "\n";
    
    $date->setTimezone(new DateTimeZone('Pacific/Chatham'));
    echo $date->format('Y-m-d H:i:sP') . "\n";
    ?>
    

    Procedural style

    <?php
    $date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
    echo date_format($date, 'Y-m-d H:i:sP') . "\n";
    
    date_timezone_set($date, timezone_open('Pacific/Chatham'));
    echo date_format($date, 'Y-m-d H:i:sP') . "\n";
    ?>
    

    The above examples will output:

    2000-01-01 00:00:00+12:00
    2000-01-01 01:45:00+13:45
    
    0 讨论(0)
  • 2020-11-22 04:29

    You can use the datetime object or their function aliases for this:

    Example (abridged from PHP Manual)

    date_default_timezone_set('Europe/London');
    
    $datetime = new DateTime('2008-08-03 12:35:23');
    echo $datetime->format('Y-m-d H:i:s') . "\n";
    $la_time = new DateTimeZone('America/Los_Angeles');
    $datetime->setTimezone($la_time);
    echo $datetime->format('Y-m-d H:i:s');
    

    Edit regarding comments

    but i cannt use this method because i need to show date in different time zones as the user login from different locations

    That's not a problem. When a user logs in, you determine his timezone and set it to your DateTime object just like shown. I'm using a similar approach in one of my projects and it works like a charm.

    in the database i need to get the dates in any single timezone, then only it can be processed properly

    You store the time either as a timestamp or a datetime in one timezone. When you query a DateTime field, you either convert the time in a DateTime object to this timezone or - if your db supports it - query with the selected timezone.

    0 讨论(0)
  • 2020-11-22 04:29

    An even simpler method looks like this:

    date_default_timezone_set('Europe/London'); // your user's timezone
    $my_datetime='2013-10-23 15:47:10';
    echo date('Y-m-d H:i:s',strtotime("$my_datetime UTC"));
    

    As described in the PHP manual, strtotime() accepts a timezone too, you just have to append it to your datetime.

    I recommend you to store all your datetimes in UTC because that way you won't have problems with the daylight savings.

    0 讨论(0)
  • 2020-11-22 04:29
    // Convert date from one zone to another..
    /* 
    $zone_from='Asia/Kolkata';
    
    $zone_to='America/Phoenix';
    
    date_default_timezone_set($zone_from);
    
    $convert_date="2016-02-26 10:35:00";
    
    echo $finalDate=zone_conversion_date($convert_date, $zone_from, $zone_to);
    
    */
    function zone_conversion_date($time, $cur_zone, $req_zone)
    {   
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d H:i:s");
        
        date_default_timezone_set($cur_zone);
        $local = date("Y-m-d H:i:s");
      
        date_default_timezone_set($req_zone);
        $required = date("Y-m-d H:i:s");
        
        /* return $required; */
        $diff1 = (strtotime($gmt) - strtotime($local));
        $diff2 = (strtotime($required) - strtotime($gmt));
    
        $date = new DateTime($time);
        $date->modify("+$diff1 seconds");
        $date->modify("+$diff2 seconds");
    
        return $timestamp = $date->format("Y-m-d H:i:s");
    }
    
    0 讨论(0)
  • 2020-11-22 04:32

    None of these answers worked for me (I skipped trying code that was overly bulky in size). I also think it's weird to change the default timezone just for a single conversion.

    Here is my solution:

    function changeTimeZone($dateString, $timeZoneSource = null, $timeZoneTarget = null)
    {
      if (empty($timeZoneSource)) {
        $timeZoneSource = date_default_timezone_get();
      }
      if (empty($timeZoneTarget)) {
        $timeZoneTarget = date_default_timezone_get();
      }
    
      $dt = new DateTime($dateString, new DateTimeZone($timeZoneSource));
      $dt->setTimezone(new DateTimeZone($timeZoneTarget));
    
      return $dt->format("Y-m-d H:i:s");
    }
    

    So, to convert to the server default, you would just pass one timezone:

    changeTimeZone("2016-10-24 16:28", "Asia/Tokyo");
    

    To convert from the server default to the user, you would leave the 2nd parameter null or blank:

    changeTimeZone("2016-10-24 16:28", "", "Asia/Tokyo");
    

    And to switch between 2 timezones unrelated to the default, you would provide 2 timezones:

    changeTimeZone("2016-10-24 16:28", "America/New_York", "Asia/Tokyo");
    
    0 讨论(0)
  • 2020-11-22 04:37

    UTC to local:

    <?php
    $datetime = date("Y-m-d H:i:s");
    $utc = new DateTime($datetime, new DateTimeZone('UTC'));
    $utc->setTimezone(new DateTimeZone('America/Sao_Paulo'));
    echo $utc->format('Y-m-d H:i:s');
    
    ?>
    
    0 讨论(0)
提交回复
热议问题