Question

I would like to create a DateTime Object, which should also set the correct timezone from the given W3C date string. An example of W3C date would be: 2014-03-12T09:30:04+0000

I googled that a lot and couldn´t find a solution for it.

What I got till now is the following:

$dt = new DateTime();
$dt->setTimestamp(strtotime('2014-03-12T09:30:04+0000'));
var_dump($dt);

But I think this ignores the timezone, doesn´t it?

Was it helpful?

Solution

Have you tried just this?

$dt = new DateTime('2014-03-12T09:30:04+0000');
print_r($dt);

My output:

DateTime Object
(
    [date] => 2014-03-12 09:30:04
    [timezone_type] => 1
    [timezone] => +00:00
)

So, it saves fine the timezone, but you probably are not interpreting fine the output; if you want to print it in YOUR local time, then you have to do some conversion.

OTHER TIPS

try

$dt = new DateTime('2014-03-12T09:30:04+0400');
var_dump($dt);

last 4 numbers is for defining your timezone.

outputs

object(DateTime)#1 (3) {
    ["date"]=>
    string(19) "2014-03-12 09:30:04"
    ["timezone_type"]=>
    int(1)
    ["timezone"]=>
    string(6) "+04:00"
}

in conclusion no it does not ignore timezone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top