12

i am new to php. i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.

0001
  • 141
  • 1
  • 1
  • 5
  • read php manual for [date](http://in2.php.net/manual/en/function.date.php) and example given there – xkeshav Mar 28 '11 at 04:46

3 Answers3

33

Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.

Here's code to switch to GMT

$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));

and back to the default timezone...

$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.

e.g.

// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');

// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');

// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));

// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');

// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');

There's a whole lot of functions you can use, that are much more readable that the procedural functions.


Explicit example of converting from a timezone to GMT

$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');

$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');

Using your Asia/Kolkata to America/New_York

date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Jacob
  • 8,278
  • 1
  • 23
  • 29
  • thanks jcob......but please can u post some example how exactly it works............ – 0001 Mar 28 '11 at 05:01
  • thank u so much jcob......but i am getting confused here........how your example will help me in my question......?? – 0001 Mar 28 '11 at 05:17
  • @0001 in my first 2 code snippets. First one creates a DateTime instance for the current time using the default timezone (set in php.ini or date_default_timezone_set()) and converts it to GMT. Then you can echo it using `$date->format()`. The second snippet create a DateTime instance in GMT, and converts it to the default timezone. – Jacob Mar 28 '11 at 05:21
  • hey jcob.........i tried ur code......i am getting '2011-12-24 13:00:00'as a GMT convertion of '2011-12-25 00:00:00' and for original dateformat i used date_default_timezone_set('Australia/Melbourne'); i am getting 2011-01-01 11:00:00 as original data.getting confused here. – 0001 Mar 28 '11 at 05:42
  • @0001 Using just the last code snippet, it converts to GMT outputs 2011-12-24 13:00:00, then converts back to melbourne... outputs 2011-12-25 00:00:00. – Jacob Mar 28 '11 at 05:48
  • Looks like you are using my first example... you should not be creating two different date times... The first 2 code snippets are a one time example, not a function that will convert anything. – Jacob Mar 28 '11 at 05:53
  • one more question jcob.....please dont mind....here whether DST is used in your code?bcoz still DST concept i not clear to me. – 0001 Mar 28 '11 at 06:01
  • @0001 DateTime will handle DST for you. – Jacob Mar 28 '11 at 06:04
  • hey i am facing problem here.....i am trying to check your code with America/NewYork time zone,,,,,,,i gave today's date and time as input...2011-03-28 13:00:00...as DST is started in US now....i should get 2011-03-28 03:30:00 as the ouput right??but im getting 2011-03-28 17:00:00....dont know where is the problem – 0001 Mar 28 '11 at 07:01
  • @0001 What is your default timezone? where is your server? – Jacob Mar 28 '11 at 07:26
  • @0001 try with your logic too, don't be dependent on SO – xkeshav Mar 28 '11 at 07:57
  • ur code works fine jacob......but here in ur code not converting it into GMT format.directly it will show America/NewYork time zone .I want their Gmt converstion also along with America/NewYork time zone – 0001 Mar 28 '11 at 09:19
5

Use the gmdate function to convert to GMT time.

For example

$d = '2011-03-28 12:05:20'; 
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
0

// Convert local time to gmt

    public function convertTime($timezone,$time){
        $selectedtime = date("Y-m-d H:i",strtotime($time));
        $date = new DateTime($selectedtime, new DateTimeZone($timezone));
        $date->setTimezone(new DateTimeZone('GMT'));
        $convertedtime = strtotime($date->format('Y-m-d H:i'));
        return $convertedtime;
    }
rushil
  • 591
  • 7
  • 16