0

My timestamp is like this:

$timestamp = "2018-08-28T08:49:44+00:00";

Easy to convert this to a unix timestamp:

$unixtime = strtotime($timestamp);
echo $unixtime; // result: 1535446184

My desired result is to get the timestamp with timezone for my current timezone. It should be like this:

$timestamp_with_timezone = "2018-08-28T10:49:44+02:00";

However if I do:

echo date('c',$unixtime);

The result is again:

2018-08-28T08:49:44+00:00

What is the correct way to get the desired local time date in ISO 8601 format?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
  • Yes, but what do you "really" want? ;-) The time with the time-zone of where the server is located, or the time-zone where the user is located? The first would require you to know the time-zone settings of the server which is running your PHP website, and the second would require the user to send information about the user's location (or perhaps they have a user-preferences table in the database where they store their preferred time-zone???). – cartbeforehorse Aug 28 '18 at 09:28
  • True I should have clarified this even more. However this is for modifiying the publish date of news articles therefore I need the timezone of the country where the website is hosted: In WordPress I can get it with `get_option('timezone_string')` – Blackbam Aug 28 '18 at 09:32
  • If I need the server time I could use `date_default_timezone_get()` and if I need the users time it entirely depends on from where I get it, just for the interested. – Blackbam Aug 28 '18 at 09:42

2 Answers2

4

Using the DateTime class:

// DateTime automatically parses this format, no need for DateTime::createFromFormat()
$date = new DateTime("2018-08-28T08:49:44+00:00");

// Set the timezone (mine here)
$date->setTimezone(new DateTimeZone('Europe/Paris'));

// Output: 2018-08-28T10:49:44+02:00
echo $date->format('c');

Blackbam
  • 17,496
  • 26
  • 97
  • 150
AymDev
  • 6,626
  • 4
  • 29
  • 52
  • @Blackbam thanks for the edit ! I first wrote this on a single line, forgot to remove the echo when pasting and splitting my code. – AymDev Aug 28 '18 at 09:35
1

You can set the TimeZone of the DateTime object:

$timestamp = "2018-08-28T08:49:44+00:00";

$date = date_create($timestamp);

$date->setTimezone(new DateTimeZone('Europe/Amsterdam'));

echo date_format($date, 'Y-m-d H:i:sP') . "\n";
Tom
  • 3,281
  • 26
  • 33