how to convert php date formats to GMT and vice versa? -


I'm new to php. I want to write a function where I want to get the DST, GMT format and later back to the original entry In the format, any date format requires the user for the date of input. Any body help me

Although gmdate functions are available, if you are using PHP 5.2 or higher, the object Consider using it.

There is a code to switch to GMT

  $ date = new dateTime (); $ Date-> Set Timezone (New DateTimeZone ('GMT'));   

and return to the default timezone ...

  $ date = New date time ('2011-01-01', New DateTime Zone ('GMT' )); $ Date-> Settime (new datetime time zone (date_default_timezone_get ()));   

By using the date time object, you can create a date time, just like procedural tasks, except that you live in the context of an example.

For example

  // Go to the context of the Christmas 2011 Christmas lunch = date = new date time ('2011-12-25 13:00 : 00 '); // Print the date to see the people, whatever format we specify, transcript $ date- & gt; Format ('d jsm y'); // Change the time zone to GMT $ date- & gt; Set Timezone (New DateTimeZone ('GMT')); // Now print the date / time fixed in GMT timezone //, as opposed to the default timezone it was created. Transcript $ date- & gt; Format ('Y-M-DH: I: S'); // To show just a few more, get $ date last Sunday- & gt; Modify ('Last Sunday');   

There are many functions you can use, which are more readable that procedural work.


GMT from a timezone $ Melbourne = new datetime zone ('Australia / Melbourne'); $ GMT = New DateTimeZone ('GMT'); $ Date = new date time ('2011-12-25 00:00:00', $ melbourne); $ Date & gt; SetTimezone ($ GMT); Transcript $ date- & gt; Format ('Y-M-DH: I: S'); // Output: 2011-12-24 13:00:00 // On Christmas Eve in Melbourne, it will be at 1 a.m. on Christmas Eve at midnight. Echo & lt; Br / & gt; '; // it back to Australia / Melbourne; $ date-> settimejone ($ melbourne); Transcript $ date- & gt; Format ('Y-M-DH: I: S');


Use of America / New York from your Asia / Kolkata

  date_default_timezone_set ('Asia / Kolkata'); $ Date = new date time ('2011-03-28 13:00:00'); $ Date-> SettimeZone (new dated datezone ('America / New York')); Transcript $ date- & gt; Format ("Y-M-DH: I: S"); // Output: 2011-03-28 03:30:00    

Comments