简体   繁体   中英

PHP date_create function, converting UTC time to different timezone using the timezone abbrev

$placename=timezone_name_from_abbr("", $timezone * 3600, false);
                        $date = date_create($list['stamp'], timezone_open($placename));
                        $datetimezone=new DateTimeZone('Europe/London');
                        $dateTime = new DateTime($list['stamp'],$datetimezone); 
                        $dateTime->setTimeZone(new DateTimeZone($placename)); 

edit

I tried the following code suggested as an answer, and it produced a time that was 2 hours off what it should have been. I checked my db and the time in the DB matches GMT. so the issue is with converting it from GMT using the conversion

$timezone=-4

code from users answer, below produces time that is two hours off. converts from GMT to America/New_York but it two hours slow. I checked the placename variable to make sure that it is America/New_York. what could be causing this?

$placename=timezone_name_from_abbr("", $timezone * 3600, true);
$dateTime = new DateTime($list['stamp']); 
$dateTime->setTimeZone(new DateTimeZone($placename)); 
echo $dateTime->format('F d g:i a'); 

original question

instead of converting it to America/New_York time this produced a UTC time that i have stored in my database, why is this, why didnt it convert the time? it is stored in db as 2012-05-13 07:30:47

$placename=timezone_name_from_abbr("", $timezone * 3600, true);
$date = date_create($list['stamp'], timezone_open($placename));
echo date_format($date, 'F d g:i a');
$dateTime = new DateTime($date,new DateTimeZone('Europe/London')); 
$dateTime->setTimeZone(new DateTimeZone('America/New_York')); 
echo $dateTime->format('F d g:i a'); 

Note that I added the second parameter upon creation of the DateTime object to ensure that it knows that what I'm passing is a GMT time, other ways it will assume its your servers local time and may end up with different results.

To your first version without the quotes.

By the way, you're assigning (on your code) a string without quotes, this may or may not result in an error since PHP will try to look for a constant called America and divide it by a constant name New_York but I'm assuming thats not what you're after.

If you've all the warning/notices/errors showing you'd see something on the lines of this:

Notice: Use of undefined constant America - assumed 'America' in line 3;

Which, if you want to think about it would end up returning an actual warning (those two prior ones were notices) telling you your diving by zero. PHP is dangerous in the sense that it lets you do all sorts of crazy stuff without actually breaking anything apparently. Don't get me wrong, I love PHP, you just need to be careful at first.

The code I gave you works :)

If you don't specify a timezone upon creation (as a second parameter of the constructor) it will assume that the timezone is your current timezone (the servers one), you can override this by passing the GMT one over there or whatever you've stored in the database.

However, there is a warning on the docs that says: Note:

The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (eg @946684800) or specifies a timezone (eg 2010-01-28T15:00:00+02:00).

Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM