Abhijeet Dange
Abhijeet Dange

Reputation: 133

How to convert different time zone date & time to GMT time in php

I have following date formats

$date_format1 = "Sat, 31 May 2014 00:00:00 +0200";
$date_format2 = "Mon, 02 Jun 2014 14:00:00 -0400";
$date_format3 = "Mon, 02 Jun 2014 11:03:00 BST";
$date_format4 = "Mon, 02 Jun 2014 10:03:00 EDT";

What will be the PHP code to convert all above formats at GMT. Note: I required only single(If possible) function to handle all above format.

Upvotes: 2

Views: 1346

Answers (5)

RiggsFolly
RiggsFolly

Reputation: 94672

The gmdate() function will output the date/time in GMT and the strtotime() function will convert your datetime strings to a valid parameter for gmdate().

$date_format1 = "Sat, 31 May 2014 00:00:00 +0200";
$date_format2 = "Mon, 02 Jun 2014 14:00:00 -0400";
$date_format3 = "Mon, 02 Jun 2014 11:03:00 BST";
$date_format4 = "Mon, 02 Jun 2014 10:03:00 EDT";


$newFormat = 'd/m/Y H:i:s';


echo gmdate($newFormat, strtotime($date_format1)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format2)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format3)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format4)) . PHP_EOL;

And the results would be:-

30/05/2014 22:00:00
02/06/2014 18:00:00
02/06/2014 10:03:00
02/06/2014 14:03:00

Upvotes: 2

gtmaroc.ma
gtmaroc.ma

Reputation: 174

i create for u this function, it's easy hope it will help you

function datetoutc($d , $zone="UTC"){
    $d = new DateTime($d);
    $d->setTimezone(new DateTimeZone($zone));
    return $d->getTimestamp();
}

print datetoutc("Sat, 31 May 2014 23:00:00 +0200");

Upvotes: 0

user3669523
user3669523

Reputation: 69

This exactly gives GMT time:

gmdate('Y-m-d H:i:s', strtotime ('+1 hour')) 

Upvotes: 0

Simon Shirley
Simon Shirley

Reputation: 328

The PHP function strtotime (string $time) can be used to convert the text part of, say, $date_format1 to a timestamp that can be used in the gmstrftime() function, so:

gmdate("<insert_date_format_here>", strtotime($date_format1));

See Date Formats and Time Formats from the PHP documentation.

Upvotes: 0

h.s.o.b.s
h.s.o.b.s

Reputation: 1319

Please read about php function gmdate()

Upvotes: 0

Related Questions