PHP DateTime DST - php

I searched the PHP manual and StackOverflow too, but haven't really found a proper answer to my question. My dates are stored in UTC. Now if I do:
$date = new DateTime('2012-03-16 14:00:00', 'UTC');
$date->setTimezone('Europe/Budapest');
Will DateTime::setTimezone() set DST automatically? So if I format the string, will it output 15:00:00 in summer time and 16:00:00 in winter time? Or will I have to set DST manually?
Related question, if I want to get a DST independent UTC time (that I can store), will the following work?
$date = new DateTime('now', 'UTC');
$date->format('Y-m-d H:i:s');
Or better to use simple gmdate('Y-m-d H:i:s')?

Your assumption is correct.
In addition, this would have taken you 10 seconds to verify yourself.
Edit:
The correct syntax is:
$date = new DateTime('2012-03-16 14:00:00', new DateTimeZone('UTC'));
As to your second question. The timezone passed in the DateTime constructor is the 'reference' timezone.
You should still call ->setTimeZone(new DateTimeZone('UTC')) to format it in the UTC timezone, unless the default timezone was also set to UTC (date_default_timezone_set).
Next time, try a little harder trying stuff out before asking.

Related

Converting a Time from a database to BST with PHP

So I've trying all sorts of combinations to get a date from my database (using Wordpress) to display in British Summer Time and I cannot get anything to work.
Is there any simple solution that can take the date string and make sure that in Summer Time in the UK it's an hour on from UTC time?
$classJson = $class->info;
$classJsonAsArray = json_decode($classJson, TRUE);
$classStartDate = strtotime($class->periodStart);
$classStartTime = date('H:i',$classStartDate);
So currently $class->periodStart returns: 2022-04-06 08:30:00
The time of that event should be 9.30am
All I need it to do is display the correct time, as at the moment, on the front end it displays as 8.30am.
DateTime handles timezones quite well.
$dateStringInUtc = '2022-04-06 08:30:00';
$date = new DateTime($dateStringInUtc, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s'); // will output 2022-04-06 09:30:00
Working with DateTime and timezones like in the accepted answer is the better way.
But it also works with strtotime if the timezone is appended to the date string. Date then returns the local time for a timestamp.
$utcDate = '2022-04-06 08:30:00';
echo date('Y-m-d H:i:s',strtotime($utcDate.' UTC'));

Different behavior when printing DateTime depending on how the timezone was set

Why do I get different dates depending of how I set the timezone on the two code snippets below?
// Setting timezone using setTimezone'
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-04-04 12:00:00');
$date1->setTimezone(new DateTimeZone('UTC'));
$date1->add(new DateInterval('PT7776000S'));
echo $date1->format('c') . PHP_EOL;
prints 2018-07-03T10:00:00+00:00
// Setting timezone as a param to createFromFormat
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-04-04 12:00:00', new DateTimeZone('UTC'));
$date2->add(new DateInterval('PT7776000S'));
echo $date2->format('c') . PHP_EOL;
prints 2018-07-03T12:00:00+00:00
When you instantiate DateTime without timezone information, the date is getting interpreted in whatever your default local timezone is; when you then set a new timezone, the date is getting converted to that timezone. I.e.:
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-04-04 12:00:00');
$date1 is 12:00:00 in, say, Europe/Berlin.
$date1->setTimezone(new DateTimeZone('UTC'));
$date1 is now 10:00:00 in UTC.
When you instantiate DateTime with timezone information, the date is getting interpreted as referring to a time in that timezone, and there's no conversion process afterwards. I.e.:
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', '2018-04-04 12:00:00', new DateTimeZone('UTC'));
$date2 is 12:00:00 in UTC.
It looks like your second example properly adds 90 days (7776000 seconds) t0 the original date.
I think what is happening is that in the first example you are setting the time according to your current timezone (default) and then converting to UTC and then adding the seconds.
In the first example you are setting the timezone to UTC and setting the time, then adding the timeinterval.
Look at this older question's answer:
Timezone conversion in php
Edit: Just saw #deceze already answered similarly. Still, take at the link to see other discussion/examples/etc.

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

Datetime php conversions and usage with timezone

I am trying convert a utc time stored date to another time zone but i cant seem to get it right.
I have a time :
date1 = new DateTime('first day of the month');
date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date
The default DateTime timezone is in UTC. The time i want to make reference is in 'Europe/Amsterdam' timezone. Any way i cant get the time in 'Europe/Amsterdam' timezone to be equivalent to the first day of the month time in UTC? (Uh, sorry my question was confusing.. let me just give an example to be clear). Im trying to query from a db.
If UTC date time is June 01, 2013. 00:00:00
I want to get get May 29, 2013 19:55:00.
I tried getting the difference between the two declared times with different timezones to get the time that i wanted but it seems it didnt work :(
My Edit/ Clarification:
If use this code:
$date1 = new DateTime('first day of the month');
$date1.setTime(0,0,0);
print_r($date1->format('Y-m-d H:i:s'));
I would get:
2013-06-01 00:00:00
Then if i use timezone:
$date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
print_r($date1->format('Y-m-d H:i:s'));
I would get: (This is just a sample output):
2013-06-01 03:00:00
Because of time difference. Want i want to get is like the reverse: I want to get the datetime that when converted 'UTC' timezone i would get this: 06-01-2013 00:00:00 time. So my preffered output is : 2013-05-29 21:00:00 ...
You can do in an OOP way like so.
$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";
To set the default date in PHP, you can either set it in your ini file or in a PHP file like so:
date_default_timezone_set('Europe/Amsterdam');
Then to format the date, refer to http://www.php.net/manual/en/function.date.php for formatting.
In your case this would be:
date('j M Y' time());
Where j = day, M = month and Y = year.

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