Calculate if DST active for a given timestamp and offset - php

Given a unix timestamp and timezone offset relative to UTC (e.g. -5), is it possible to calculate if DST is active?
I can do something like:
<?php
echo 'DST enabled? ' . date('I', 1345731453) ."<br>";
?>
But I cannot see how to pass in an offset or a TimeZone.
Im guessing also that there is a difference between the timezone and the offset, given that specific countries support DST?
Appreciate any thoughts.

Correct, you need to start with date and tz to determine offset.
in 5.3+ you can get transitions for start/end period, so you can just use your timestamp for both, something like this:
$inputTime = $your_unix_timestamp;
$inputTZ = new DateTimeZone('Europe/London');
$transitions = $inputTZ->getTransitions($inputTime,$inputTime);
$offset = $transitions[0]['offset'];
$offset is the DST offset at the time in question

Get the timezone with:
$old_zone = ini_get('date.timezone');
Change it to the one you want:
ini_set('date.timezone', 'Europe/Brussels');
Revert back:
ini_set('date.timezone', $old_zone);
The PHP timestamp is always UTC. Depending on your timezone setting it determine what time to show.

Related

Can someone tell me how the DateTimeZone::getOffset PHP function works?

I got this line of code:
echo (new DateTimeZone('UTC'))->getOffset(new DateTime('now', new DateTimeZone('America/New_York')));
It returns zero. But I expected it to return the difference between UTC and America/New_York time zones.
I then provided the same time zone twice:
echo (new DateTimeZone('America/New_York'))->getOffset(new DateTime('now', new DateTimeZone('America/New_York')));
I expected it to return zero because the time zones are the same. But it now returns -18000 (That's the offset between America/New_York and UTC).
In the documentation it says that getOffset() always returns the offset to GMT (=UTC). But then why is getOffset not static? And if it is always the offset relative to GMT why does the time zone in the first constructor play a role?
I know there's another getOffset method in the DateTime class which is easier to use. But i want to understand how the getOffset method in the DateTimeZone class works.
The offset of a timezone is how many hours it differs from UTC. The offset of a timezone may change throughout the year depending on when DST goes into and out of effect. So you cannot get the offset of a timezone just like that, since it's not a constant value. You can only interrogate a timezone what its offset from UTC is at a given time. That's what you pass the DateTime object to getOffset for: to ask the timezone what its offset value is for the given time. E.g.:
echo (new DateTimeZone('America/New_York'))->getOffset(new DateTime), PHP_EOL;
echo (new DateTimeZone('America/New_York'))->getOffset(new DateTime('+6 months'));
-18000
-14400
If you want to get the offset difference between two arbitrary timezones, you get their respective UTC offsets and subtract them.
I came here looking for a way to get the offset between PHP's current default timezone and UTC. I went down the rabbit hole with DateTimeZone::getOffset, but it turns out there's a much easier way:
$offset = date('O');
// Returns a string like '-0400'
$offset = date('P');
// Returns a string like '-04:00'
$offset = date('Z');
// Returns the offset in seconds, e.g. '-14400'
To be clear, this gets whatever the timezone offset is right now, but I imagine that's what is wanted most of the time.
You can switch PHP's current timezone with date_default_timezone_set().
See https://www.php.net/manual/en/datetime.format.php

ISO date formats to wrong day

Short question but I can't get my finger on it. This piece of code:
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date));
returns Mon while
$date = '2015-12-08T00:00:00';
echo date('D', strtotime($date));
returns Tue. Why is that? The +01:00 is for the timezone, but that should not affect the day in my opinion.
First I've looked up that 08-12-2015 is in fact a Tuesday, so now we know the first one is incorrect.
PHP's date() is an Unix timestamp according to their own docs.
My belief is that adding the +1 as a timezone triggers the calculation to the +0 timezone (UTC) when asking for the day of the week and therefore returns 23:00 on monday as the current UTC time.
You can specify the timezone before executing the rest of the code: date_default_timezone_set('Europe/Amsterdam');
<?php
date_default_timezone_set('Europe/Amsterdam'); //this is an example of a +1 timezone, choose one from http://php.net/manual/en/timezones.php
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date) );
?>
strtotime will parse your date string using the supplied time zone or using the default timezone if unspecified. We can't see from the code you've posted what time zone your server is configured to, but once the date is parsed and converted to your time zone, the time may legitimately occur in the previous day, hence why you're seeing 'Mon'.
Either supply a time zone in the strtotime call via the now argument or set one globally with date_default_timezone_set.

How can I apply a timezone offset provided through JS?

Let's say I detect the users timezone offset with:
var tz = (new Date).getTimezoneOffset()/60;
and send the value of -5 to PHP, so:
<?php
date_default_timezone_set('UTC');
$tz = $_POST['tz']; // -5
$date = strtotime('now');
I want to store the timestamp as UTC in the database, but when displayed to the user I want to apply the timezone offset. So assuming $tz is -5 for EST, how can I apply it to the timestamp in order to display a date('M d Y H:i') in my local timezone ?
Bonus question: How can I have it show time for PST only? I assume by finding the PST offset and just specifying a hardcoded PST number to the equation that will be used in the original answer.

PHP date() function ignores the timestamp parameter

The output of the following program can be seen here: http://codepad.org/egNGJBUL
<?php
/* Checking if time() is really timezone independent */
date_default_timezone_set('UTC');
echo time();
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo time();
echo "\n";
/* Using date() function passing timestamp parameter */
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s',time());
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo date('Y-m-d H:i:s',time());
echo "\n";
/* Using date() function without passing timestamp parameter */
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s');
echo "\n";
date_default_timezone_set('Australia/Queensland');
echo date('Y-m-d H:i:s');
echo "\n";
From line 1-2 of the output, we can see time() returns a value which is really timezone independent.
In line 3-4, it's strange that date() function ignores the timestamp parameter and still display the date time according to the timezone set.
Why is it like this?
Not really sure what you are expecting to see, but yes, looks very normal to me.
A timestamp is a integer counted from a certain point in time (usually the UNIX EPOCH). While the display of this value is timezone independent, it is no more or less so that say, the value of a properly formatted date, notated with a timezone, is timezone independent...
example, all of the following statements are both true (logically)
1297799809 == 1297799809
2011-02-15 19:56:49 (UTC) == 2011-02-16 05:56:49 (Austria/Queensland)
All time is 'timezone independant'. Timezones only affect the way we display a particular moment in time.
date() functions second parameter, if not specified, is time() value.
date() Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
from date()'s manual
So actually nothing is being ingnored.
The date function returns the date of a timestamp calculated for the current timezone, as others have said, if no timestamp is passed to it, then the current time is used for the timestamp, so passing time() is the same as not passing anything at all.
However, doing something like $time = time();sleep 5;echo date($format,$time); will get you a date 5 seconds in the past.
It's meant to display the date formatted for current timezone so you can have a universal method of keeping time that's constant across computers/servers and be easily parsable, and yet be able to display the date in any timezone desired.
The UTC timezone is actually the time that the timestamp is calculated to, more precisely, the number of seconds since 00:00 Jan 1, 1970 UTC, then it adds or subtracts 3600 (60*60) seconds from/to the timestamp per hour offset from UTC time to get the time in the currently set timezone.

adjust time zone

How would I take a stored date, like 2011-01-30 18:23:49, and adjust it to any chosen timezone? Is there a simple way such as simply defining the time zone by abbreviation or adding/subtracting x amount of hours?
Basically I want users to be able to choose their time zone and this default date be adjusted to fit theirs.
Have the user choose their time zone
Use that zone name or offset with date_default_timezone_set to set the default time zone used in date functions throughout the rest of script execution.
Use date('Z') to get that time zone's offset from GMT in seconds
Convert your stored date to a timestamp with strtotime -- UNIX timestamps are always GMT, so you now have the time in GMT.
Add the offset from step 3 to convert that time to the user's time zone.
Use date again to format the timestamp as a string in the desired display format.
Example:
$user_timezone = 'America/Los_Angeles';
$stored_time = '2011-01-30 18:23:49';
date_default_timezone_set($user_timezone);
$timestamp = strtotime($stored_time);
$local_timestamp = $timestamp + date('Z');
$local_date = date('Y-m-d H:i:s', $local_timestamp);
echo $local_date;
Here comes my solution. I tested it with America/Los_Angeles as the servers timezone, and my timezone as the users. I assume that the time is stored using the servers timezone.
<?php
// My (user) timezone
$user_timezone = 'Europe/Berlin';
// Server timezone
$stored_timezone = 'America/Los_Angeles';
// Date/Time stored in your DB, using timezone of the server (yours, that is)
$stored_datetime = '2011-01-29 22:40:00'; // this is the current time in L.A.
// Setting default to servers timezone if not done before
date_default_timezone_set($stored_timezone);
// converting to unix timestamp
$stored_timestamp = strtotime($stored_datetime);
// setting default to users timezone
date_default_timezone_set($user_timezone);
// converting to timestamp
$user_datetime = date('Y-m-d H:i:s', $stored_timestamp);
// setting default back to servers timezone
date_default_timezone_set($stored_timezone);
echo $user_datetime; // output is my current time

Categories