converting a time to a different timezone with php

前端 未结 6 1994
鱼传尺愫
鱼传尺愫 2020-12-11 17:38

$timeposted = \"7:10pm\";

This value is currently Canada time (quebec). I\'m trying to find a way to convert it to France\'s time. How can i do that ?

相关标签:
6条回答
  • 2020-12-11 17:56

    Assuming that your PHP configuration is set to the Quebec time, you can convert it to France's timezone by doing the following:

    $date = new DateTime('7:10pm', new DateTimeZone('Europe/Paris'));
    echo $date->format('Y-m-d H:i:sP');
    

    Or, if your server is not set to the Quebec timezone you can:

    $date = new DateTime('7:10pm', new DateTimeZone('America/Montreal'));
    
    $date->setTimezone(new DateTimeZone('Europe/Paris'));
    
    echo $date->format('Y-m-d H:i:sP');
    

    which returns

    2013-06-14 01:10:00+02:00 
    

    You can read more about PHP and timezones here: http://www.php.net/manual/en/datetime.settimezone.php

    0 讨论(0)
  • 2020-12-11 17:59

    This is my function that take time from a mysql db (which I've stored entirely in UTC) and converts to a new timezone and formats it simply.

    function changetimefromUTC($time, $timezone) {
        $changetime = new DateTime($time, new DateTimeZone('UTC'));
        $changetime->setTimezone(new DateTimeZone($timezone));
        return $changetime->format('m/d/y h:i a');
    }
    

    This is a list of supported timezones http://us1.php.net/manual/en/timezones.php

    0 讨论(0)
  • <?php
    date_default_timezone_set('America/Los_Angeles');//Your global default timeZone.
    
    function convertTimeZone($oTime, $oTimeZone, $nTimeZone) 
    {
    //Parameter string $oTime is original time to be converted from in format F-d-Y h:i:s
    //Parameter string $oTimeZone is timezone to be conveted from- Timezone of $oTimeZone
    //Parameter string $nTimeZone is timezone to be conveted to
    
    date_default_timezone_set($oTimeZone);  //Change default timezone to old timezone within this function only.
    
    $originalTime = new DateTime($oTime);
    
    $originalTime->setTimeZone(new DateTimeZone($nTimeZone)); //Convert to desired TimeZone.
    
    date_default_timezone_set('America/Los_Angeles') ; //Reset default TimeZone according to your global settings.
    
    return $originalTime->format('F-d-Y h:i:s A'); //Return converted TimeZone.
    } 
    
    $LATime = convertTimeZone("2011-01-07 19:55:00","America/Chicago", "America/Los_Angeles");
    
    echo $LATime;
    
    ?>
    
    0 讨论(0)
  • 2020-12-11 18:04

    Check out DateTime::setTimezone:

    Example

    date_default_timezone_set('America/Los_Angeles');
    
    $datetime = new DateTime('2013-06-13 12:35:23');
    echo $datetime->format('Y-m-d H:i:s') . "\n";
    $timeEurope = new DateTimeZone('Europe/London');
    $datetime->setTimezone($timeEurope);
    echo $datetime->format('Y-m-d H:i:s');
    
    0 讨论(0)
  • 2020-12-11 18:05

    You can use date_default_timezone_set function to change loacal time zone

    Example

    date_default_timezone_set('Europe/Paris');
    
    0 讨论(0)
  • 2020-12-11 18:10

    Use the date_default_timezone_set() function of PHP.

    If you want to change it to France you would use the

    date_default_timezone_set('Europe/Paris');
    

    a list of Supported Timezones can be found here: http://www.php.net/manual/en/timezones.php

    The functionality of date_default_timezone_set() can be found here: http://php.net/manual/en/function.date-default-timezone-set.php

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