51

I am trying to convert time between current time to UTC and UTC to current time zone.

Here is what I have done:

$schedule_date = new DateTime($triggerOn, new DateTimeZone('UTC') );
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn;

The output value does not change the only thing that changes in format.

the string $triggerOn was generated based on America/Los_Angeles timezone

This is how my string looks like before and after:

BEFORE    04/01/2013 03:08 PM
AFTER     2013-04-01 15:08:00

So the issue here is that DateTime does not convert to UTC.

Mike
  • 23,542
  • 14
  • 76
  • 87
Jaylen
  • 39,043
  • 40
  • 128
  • 221

3 Answers3

103

What you're looking for is this:

$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';

echo $triggerOn; // echoes 04/01/2013 03:08 PM

$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn =  $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn; // echoes 2013-04-01 22:08:00
Mike
  • 23,542
  • 14
  • 76
  • 87
  • 2
    just fyi to readers: `setTimeZone` changes the object's time zone string as well as the datetime value. format is just what is used to obtain the string. It's not like `setTimeZone` only changes the time zone property, adn then format spits it out only - the actual numeric time value got affected upon executing `setTimeZone`. – ahnbizcad Apr 04 '17 at 00:47
  • @ahnbizcad 04/01/2013 03:08 PM in Los Angeles is exactly the same time as 2013-04-01 22:08:00 in UTC. The time itself does not change, only the timezone. – Mike Apr 04 '17 at 01:55
  • I'm talking about as opposed to 12:00 eastern being changed to 12:00pacific. Setting the timezone with that function doesn't do that. I was clarifying a potential paradigm confusion. – ahnbizcad Jul 14 '17 at 22:50
  • @ahnbizcad Your previous comment is very confusing and I would even argue that it's wrong. In PHP, the date/time information is stored internally in UTC and therefore when changing the timezone, the date/time value does not change, only the UTC offset does. I think it's quite obvious from the code comments in the answer that when changing the timezone of the DateTime object that this changes the local time. – Mike Jul 15 '17 at 00:10
  • 1
    if the timezone part is called "timezone", then what would you call the other part if not "time"? The trouble is that the word "time" is ambiguous. It can refer to the actual underlying conceptual point in time, or it could be the exact digits displayed in contrast to the timezone string. I'm already thinking of the two substrings, whereas time to you means the conceptual time because we have different paradigms. If i'm talking about two substrings, then the words click to mean that one substring. The speaker/author decides the context/paradigm. Give me another word and i'll use it. – ahnbizcad Jul 17 '17 at 18:40
  • @ahnbizcad - Instead of saying *"**actual** numeric time value"* **changes**, say that `format` (or other string output) will display time in the newly specified time zone (representing the **unchanged** *actual* time). Mike's example would be clearer if it also showed output *with* timezone: `$schedule_date->format('Y-m-d H:i:s T');` or `$schedule_date->format('Y-m-d H:i:s O');` both before and after. Then we would clearly see that they are equivalent representations of an unchanged time. – ToolmakerSteve Apr 17 '19 at 11:48
17

You are consuming the date/time and setting the time zone correctly, however before formatting the datetime, you are not setting the desired output timezone. Here is an example which accepts a UTC time zone, and converts the date/time to the America/Los_Angeles time zone:

<?php
$original_datetime = '04/01/2013 03:08 PM';
$original_timezone = new DateTimeZone('UTC');

// Instantiate the DateTime object, setting it's date, time and time zone.
$datetime = new DateTime($original_datetime, $original_timezone);

// Set the DateTime object's time zone to convert the time appropriately.
$target_timezone = new DateTimeZone('America/Los_Angeles');
$datetime->setTimeZone($target_timezone);

// Outputs a date/time string based on the time zone you've set on the object.
$triggerOn = $datetime->format('Y-m-d H:i:s');

// Print the date/time string.
print $triggerOn; // 2013-04-01 08:08:00
Joshua Burns
  • 8,268
  • 4
  • 48
  • 61
4

Create the date using the local timezone, then call DateTime::setTimeZone() to change it.

Martin
  • 22,212
  • 11
  • 70
  • 132
Jerry
  • 3,391
  • 1
  • 19
  • 28
  • How can I usersetTimezone to change it: $triggerOn is created by the local time and I want to convert it to UTC. – Jaylen Mar 25 '13 at 22:21
  • 1
    Not sure why the downvote, considering the answer marked as correct does exactly this. It lacked detail, I admit, then this is a pretty straightforward question. – Jerry Mar 25 '13 at 23:01