PHP strtotime timezone - php

i have problem with PHP strtotime converting to different timezone.
My code is simple:
<?php
date_default_timezone_set("Europe/Bratislava");
echo date("H:i",strtotime("20:00 America/New_York"));
?>
This code is returning: 02:00.
But it should return 14:00.
Can you help me please to fix this?

This code is returning: 02:00. But it should return 14:00.
No, it's right. You're converting from 8pm in New York (currently UTC-4 due to DST) into the Europe/Bratislava time zone (currently UTC+2 due to DST).
So:
New York: 8pm
UTC: Midnight
Bratislava: 2am
If you're trying to convert from a Europe/Bratislava time into a New York time, then you need to switch your time zone IDs.

Related

Setting PHP DateTime doesn't appear to be observing TimeZones

I'm fairly new to PHP so forgive me if this is a stupid mistake that I haven't spotted
I've run into a problem where in our current system where we currently used strtotime and it was returning our date an hour ahead than it actually was set. E.g 1:15pm became 2:15pm when I set the timezone to be European rather than GMT.
I read that strotime had this problem but I can't get it to observe a different timezone if I try and set it.
So I tried working with PHPs DateTime instead.
The user enters the time and they select it as 1:15PM however we want to store it as 13:15. So I did this:
$t = DateTime::createFromFormat('h:i A', $venue['startTime']);
$t_24 = $t->format('H:i:s');
Then I try and create my Date object
$d = DateTime::createFromFormat('d-m-Y H:i:s', $venue['startDay'] . ' ' . $t_24);
$d->setTimezone(new DateTimeZone('America/New_York'));
echo ' ' . $d->getTimestamp();
Trying to set the timezone after the object is set because apparently it doesn't work if you add the timezone as the third argument in createFromFormat
My computers time is currently observing European time currently GMT+1 because we're observing daylight savings time in the UK, I select the time set on the through our system as 1:15pm and because I've set the timezone I expect the timestamp outputted equivalent to 7:15am as it's six hours behind European time, however when I convert the timestamp 1500639300 it's still equal to 2:15 PM. Probably done something stupid but can't quite figure out what? Any help would be appreciated :)
Timestamps have no time zone - they are always in UTC time. If you want to save timezone related data use another format! For example save in H:i:s, as you need it.
you can use gmdate() Function for this
<?php $current_time = gmdate('Y-m-d H:i:s'); ?>
http://php.net/manual/en/function.gmdate.php

Why PHP IntlDateFormatter returns wrong date? +1 hour

When I try to format date from one timezone to same timezone I get wrong time (differenced by 1 hour). My code is:
$formatter = new IntlDateFormatter(
'ru_RU',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Moscow'
);
$date = new DateTime("2015-07-29 14:00:00", new DateTimeZone('Europe/Moscow'));
echo $formatter->format($date);
Expected time is 14:00, but 15:00 returns.
UPDATE: When I display year 2013 or 2014 everything is ok! What happens in 2015?
UPDATE2: +1 hour added from 26 october 2014. Its time when Russia goes to eternal winter time. Ok. But... how to format correctly?
I use directly 'GMT+03:00' instead 'Europe/Moscow'.
Apparently the developers of the library do not have time for lawmakers.
If you have same problem, update icu (International Components for Unicode) at your OS.
Check your php.ini file for date.timezone and make sure its set to
date.timezone = "Europe/Moscow"

strtotime() returns different Unix timestamp in different timezones for string "now"

php > echo strtotime("now America/New_York")."\n";
1376459035 // -> 2013-08-14 05:43:55 - Wrong
php > echo strtotime("now UTC")."\n";
1376444635 // -> 2013-08-14 01:43:55 - OK
php > echo time()."\n";
1376444635 // -> 2013-08-14 01:43:55 - OK
Can anyone explain?
Is this some PHP's invention – timezone-"corrected" unix timestamps?
// edit:
I realize it makes no sense to even specify the timezone with "now". It does, however, with other relative times, e.g. "tomorrow midnight". There, depending on the timezone, "tomorrow" could be a day further away, depending on whether the timezone is over midnight already. The behavior is equally weird, just a bit harder to explain.
As explained on Unix time wiki, Unix Epoch is always in UTC. Thats why outputs from
echo strtotime("now UTC");
echo time();
are the same. According this info New York time zone is UTC -5 hours. With current daylight saving time +1 now it equally UTC -4 hours. That's why you got result 2013-08-14 05:43:55 (4 hours diff).
you are using
strtotime("now America/New_York") thats why it is showing unix timestamp in different timezone for string.
use this:
date_default_timezone_set("America/New_York");

DateTime->format("U") doesn't return a value with timezone info

In PHP 5.2, I'm using the following code to get a timestamp from a DateTime object.
$dateTime = new DateTime("now", new DateTimeZone("America/Los_Angeles") );
echo $dateTime->format("U");
the problem is that format("U") simply returns server timestamp, which is UTC.
How do I make it to return a timestamp from Pacific Time Zone (Los Angeles) ?
Your concept for timestamp is not right, timestamp is timezone independent, it is defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970.
Try setting timezone at top of PHP script. I think timestamps are always UTC. Use date() function to format it into what you need.
// set timezone to pacific time
date_default_timezone_set('America/Los_Angeles');

php DateTime timezone for Columbus Ohio

Columbus, Ohio is in EDT timezone but PHP::DateTime uses these php timezones and I cannot seem to find one that gives me correct time. America/New_York is off by an hour because until EDT ends we are +5 not 4.
I am confused. For example right now its 11:26AM roughly speaking and I get back 10:26AM. The server has the right time set on it, I tried using date_default_timezone_set("EDT") but this is not allowed.
$fromZone = 'Europe/London';
$toZone = 'American/New_York';
function adjustTime($time, $fromZone, $toZone){
$date = new DateTime($time, new DateTimeZone($fromZone));
$date->setTimezone(new DateTimeZone($toZone));
return $date->format('Y-m-d H:i:s');
}
For example: I receive from a soap web service "2010-09-23 15:25:56" which is Europe/London time and it should be transformed to "2010-09-23 11:25:56" which is EDT time. I was trying to use the function above and this is returning back "2010-09-23 10:25:56"
"America/New_York" should give you the right value, given that at the time of this writing it's 11.36 in New York too. Note that that's UTC-4 (it's currently 15:35 UTC). In standard time, you're UTC-5.
EDT isn't really a "full" time zone - it's just the daylight part of it. A time zone name like "America/New_York" gives the complete time zone implementation.
Now as to why your server is giving the wrong value, that's a different matter... if you ask for the UTC time, what does it give you?
EDIT: As mentioned in the comments, the fix is to convert from UTC, not from Europe/London:
$date = new DateTime($time, new DateTimeZone('UTC'))
(There may be a better way of doing it; I'm not a PHP developer.)

Categories