Time RFC3339 format, to timezone.

Viewed 2609

I need to convert this time to UTC+00 timezone time. It gives me a date with timezone depending from where user is in RFC3339 format. So it looks like this

2000-01-01T00:00:00+02:00

I need to use the +02 timezone and correctly calculate the date. So my idea is: Do I just check what is the last 00:00 and - or + days from the date? Or is there actually functions to use from php side like time_convert( ) etc..?

I've made this, how ever I am not sure if its correct way of doing it.

$date = '2000-01-01 00:00:00+02:00';
$date_date = substr($date, 0, 19);
$add_hours = substr($date, 19, 22)*3600;
$add_mins = substr($date, 23, 25)*60;
$add_secs = $add_hours+$add_mins;
$date = date('Y-m-d H:m:i', strtotime($date_date)+$add_secs);
3 Answers

gmdate is intended to format GMT(=UTC) output. See PHP: gmdate - Manual

You may get the result you want by using :

$date = strtotime("2000-01-01 00:00:00+02:00");
$utc = gmdate("Y-m-d H:i:sP", $date);

where P is the offset with GMT.

You can probably use DateTime with DateTimeZone to get what you're after.

Here's a quick example of how you may use it.

<?php


$date = new DateTime('now', new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));

echo $date->format('Y-m-d H:i:s'); // Converted to LA timezone.

You can do it so simple like:

$date = new DateTime('now')
$date->format('c')
echo $date;

Output: 2018-11-12T15:55:00+00:00

http://php.net/manual/de/function.date.php

ISO 8601 = RFC3339

Fromat = c | ISO 8601 Datum (hinzugefügt in PHP 5) Beispiel: 2004-02-12T15:19:21+00:00

Format = r | Gemäß » RFC 2822 formatiertes Datum Beispiel: Thu, 21 Dec 2000 16:01:07 +0200

Related