Anand
Anand

Reputation: 1021

Convert timezone in php

I have these timezones. I want to get the current datetime depends on the given timezone. The user will select either one timezone. so need to return current time.

ASKT
CDT
EDT
HST
MDT
MST
PDT

How can i convert? Please help

Upvotes: 0

Views: 306

Answers (3)

metalfight - user868766
metalfight - user868766

Reputation: 2750

See the class written by "the_dark_lord12001 at yahoo dot com" this will help you to get timezones from abbreviations & then you can use it with either date_default_timezone_set or DateTime class

http://www.php.net/manual/en/datetimezone.listabbreviations.php

Hope this help.

~K

Upvotes: 0

Starx
Starx

Reputation: 79059

Use the DateTime Class

$time = time(); //Get the current time
$date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display date

$date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display data again

This, way you don't have to give the same timestamp as new argument every time like mishu's answer.

Upvotes: 2

heyanshukla
heyanshukla

Reputation: 669

DateTime::setTimezone would help you.

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

Upvotes: 1

Related Questions