How to fix this very frustrating time bug in my time code? - php

I have these two functions:
function time_is_older_than($timestamp, $time_string)
{
if (strtotime($timestamp) < strtotime('-' . $time_string))
return true;
return false;
}
function time_is_younger_than($timestamp, $time_string)
{
if (strtotime($timestamp) > strtotime('-' . $time_string))
return true;
return false;
}
They enable me to do neat things like:
if (time_is_older_than($last_time_some_action_happened, '5 minutes'))
do_it_again();
They normally work, except for during one hour every six months, when my timezone switches over to "summer time" or "winter time". This means that the clocks are increased or put back one hour at midnight (according to this timezone).
The PHP manual states this for strtotime:
The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.
However, if I provide the exact same date/time string, with "+08:00" added in the end versus "+00:00", for example, I get different numbers of seconds returned. So strtotime() does understand timezones when it parses the provided time, even if the returned integer obviously doesn't contain this information. (Nor is it expected or required to by me.)
I've spent countless hours trying to debug this, testing countless things, and just sitting here thinking, but I can't figure out what exactly would make the code I have fail, specifically for one hour. And especially what about it I need to change. Setting the second parameter for strtotime() seems likely, but I just couldn't make it work correctly.
My hottest "lead" for quite some time was that the strtotime('-' . $time_string) part is ending up using a different timezone than the timestamp strings provided, but I do provide timezone data to it most of the time! An example of $last_time_some_action_happened might be something like 2020-10-28 02:22:41.123456+01.
I set the timezone with date_default_timezone_set().
I suspect that I only need to make some very minor change, but I've been experimenting so much and so long now, even taking rests in between, that my brain can no longer see this clearly. I bet the solution is something awfully simple.
Please don't tell me to use DateTimeImmutable. This would fundamentally change my entire structure and require me to do things very differently. Perhaps I should, and even will, at some point, but for now, I just wish to fix this rare but still very annoying bug in my existing code. (If it's possible at all, which I very much believe is the case.)

I'm able to reproduce the issue you are having:
date_default_timezone_set('Pacific/Auckland');
// Daylight saving time 2020 in New Zealand began at 2:00am on Sunday, 27 September
$current = strtotime('2020-09-27 02:04:00');
$d1 = strtotime('2020-09-27 02:05:00', $current);
$d2 = strtotime('-5 minutes', $current);
var_dump($d1 > $d2); // false
var_dump(date('Y-m-d H:i:s', $d1)); // 2020-09-27 03:05:00
var_dump(date('Y-m-d H:i:s', $d2)); // 2020-09-27 03:59:00
This person looks to be having the same issue as you and may appear to be a bug.
DateTime::modify and DST switch
The solution is to convert the dates to UTC then compare:
// Convert to UTC and compare
$d1 = new \DateTime('2020-09-27 02:05:00', new \DateTimeZone('Pacific/Auckland'));
$d2 = new \DateTime('2020-09-27 02:04:00', new \DateTimeZone('Pacific/Auckland'));
$d2->setTimezone(new \DateTimeZone('UTC'));
$d2->modify('-5 minutes');
$d2->setTimezone(new \DateTimeZone('Pacific/Auckland'));
var_dump($d1 > $d2); // true
var_dump($d1->format(\DateTimeInterface::RFC3339_EXTENDED)); // 2020-09-27T03:05:00.000+13:00
var_dump($d2->format(\DateTimeInterface::RFC3339_EXTENDED)); // 2020-09-27T01:59:00.000+12:00
I've updated your functions:
function time_is_older_than($datetime, $time_string)
{
$d1 = new \DateTime($datetime);
$d1->setTimezone(new \DateTimeZone('UTC'));
$d2 = new \DateTime();
$d2->setTimezone(new \DateTimeZone('UTC'));
$d2->modify('-' . $time_string);
return $d1 < $d2;
}
function time_is_younger_than($datetime, $time_string)
{
$d1 = new \DateTime($datetime);
$d1->setTimezone(new \DateTimeZone('UTC'));
$d2 = new \DateTime();
$d2->setTimezone(new \DateTimeZone('UTC'));
$d2->modify('-' . $time_string);
return $d1 > $d2;
}

Could you consider a solution:
In the timestamp string(like Thu, 21 Dec 2000 16:01:07 +0200), add a timezone tag which specify timezone without difference of daylight saving time.

Related

My PHP script for comparing dates is not working properly

I have 2 dates in the database. One is in the past, and other is in the future. I want to check if the current time is between those two dates. Everything works locally, but not on the server.
The server is in a different timezone, but if I echo the results (in comments), everything shows as it should. Dates in the database are saved in the correct timezone, so I do not apply setTimezone to those.
$timezone = new DateTimeZone('Europe/Belgrade');
$start_date = new DateTime($popup_start);
$end_date = new DateTime($popup_end);
$current_date = new DateTime();
$current_date->setTimezone($timezone);
//echo $start_date->format('Y-m-d H:i:s') .'<br>';
//echo $end_date->format('Y-m-d H:i:s') .'<br>';
//echo $current_date->format('Y-m-d H:i:s') .'<br>';
if($start_date < $current_date AND $end_date > $current_date) {
echo 'true';
}
else {
echo 'false';
}
The code above always goes to false for some reason.
Any help is appreciated
DateTime() is compared in UTC time reference (not based on format('Y-m-d H:i:s')).
The universal time reference (probably Unix Epoch seconds) is set on the DataTime instancing and does not change by ->setTimeZone(), that just affects the output, but it's still the same UTC time.
You have no TZ reference in the DB, so $start_date gets your local TZ when parsed. Let's say TZ_REMOTE is the timezone of the datestimes in the DB, and TZ_LOCAL is the local TZ. Then what happens is (pseudocode):
$start_date <- (($popup_start TZ_LOCAL)=>UTC)
$current_date <- (now()=>UTC)
So you have not gotten rid of the TZ difference in any way, it's still TZ_LOCAL dates.
What you can do is compare the textual representations, not the DateTimes:
if($popup_start < $current_date->format('Y-m-d H:I:s') && $popup_end > $current_date->format('Y-m-d H:I:s'))
Because, like I said, ->setTimeZone() will not alter the actual time, but it will alter the output of ->format() to convert the UTC datetime to the specified TZ.

Add 5 minutes difference

I have a problem.
I have a certain date/time (A), which is put in a variable. Lets say its May 06 2014 14:26.
I want to compare this date/time with file's modification date/time (B), and if it is the same, I put the file's modification date/time (B) in variable too.
The thing is, I cant get exactly the same date on file (B). So, I want that it would add 5 minutes difference. For example, if file (B) has modification date May 06 2014 14:28, it will be put in a variable. Or if it has modification date May 06 2014 14:22, it will be put in a variable too.
If it helps, I am using date("F d Y H:i",$file['filetime']) command to get the modification date of the file.
Any possibility to do this? Thanks in advance.
If something is unclear, just tell me. I'm new here, so might be hard to explain things correctly.
Maybe, it helps:
$datetimeA = strtotime($file1['filetime']);
$datetimeB = strtotime($file2['filetime']);
$interval = abs($datetimeA - $datetimeB);
$minutes = round($interval / 60);
if ($minutes > 5) {
//do some magic here
}
If $file['filetime'] is a timestamp already, then You can use it without calling strtotime().
With the DateTime class, it is done in a few lines :
$datetime1 = new DateTime('2009-10-13 10:00:00');
$datetime2 = new DateTime('2009-10-13 10:20:00');
$interval = $datetime1->diff($datetime2);
$yourIntervalle = $interval->format('%i minutes'));
i'd either directly use unix-timestamps or convert A and B to Unix Timestamps (e.g. using strtotime) and compare them based on your requirements.
if you have access to the file directly you could even use filemtime to directly get the last modified time as an unix timestamp.
Guess this is what you're looking for, if your problem is just to add 5 minutes:
$dateB = new DateTime('2014-05-06 14:26:00');
$interval = new DateInterval('PT5M');
$dateB->add($interval);
print_r($dateB);
PHPSandbox: http://sandbox.onlinephpfunctions.com/code/9f9a2ee40f09f953c796c9c2dd7e15ad62b45772
Also, look at DateInterval to understand a little more:
http://www.php.net/manual/en/dateinterval.construct.php
you can do like. if you want to minus 5 minutes from the date
$given_date = 'May 06 2014 14:28';
$date = new DateTime($given_date);
//subtract 5 minutes from the date
$new_date = $date->sub(new DateInterval('PT' . '5' . 'M'));
echo $new_date->format('F d Y H:i');
if you want to add 5 minutes then change
//add 5 minutes to the date
$new_date = $date->add(new DateInterval('PT' . '5' . 'M'));
First of all, i think you need to compare the two date/time by the method strtotime. With that you can compare the two date.
Then, you just need to make one or two insert in your database if i understand correctly with a If/Else method.
Hope it helps!

PHP adding exact weekdays to a timestamp

I want to add an x number of week days (e.g. 48 weekday hours) to the current timestamp. I am trying to do this using the following
echo (strtotime('2 weekdays');
However, this doesn't seem to take me an exact 48 hours ahead in time. For example, inputting the current server time of Tuesday 18/03/2014 10:47 returns Thursday 20/03/2014 00:00. using the following function:
echo (strtotime('2 weekdays')-mktime())/86400;
It can tell that it's returning only 1.3 weekdays from now.
Why is it doing this? Are there any existing functions which allow an exact amount of weekday hours?
Given you want to preserve the weekdays functionality and not loose the hours, minutes and seconds, you could do this:
$now = new DateTime();
$hms = new DateInterval(
'PT'.$now->format('H').'H'.
$now->format('i').'M'.
$now->format('s').'S'.
);
$date = new DateTime('2 weekdays');
$date->add($hms);//add hours here again
The reason why weekday doesn't add the hours is because, if you add 1 weekday at any point in time on a monday, the next weekday has to be tuesday.
The hour simply does not matter. Say your date is 2014-01-02 12:12:12, and you want the next weekday, that day starts at 2014-01-03 00:00:00, so that's what you get.
My last solution works though, and here's how: I use the $now instance of DateTime, and its format method to construct a DateInterval format string, to be passed to the constructor. An interval format is quite easy: it starts with P, for period, then a digit and a char to indicate what that digit represents: 1Y for 1 Year, and 2D for 2 Days.
However, we're only interested in hours, minutes and seconds. Actual time, which is indicated using a T in the interval format string, hence we start the string with PT (Period Time).
Using the format specifiers H, i and s, we construct an interval format that in the case of 12:12:12 looks like this:
$hms = new DateInterval(
'PT12H12M12S'
);
Then, it's a simple matter of calling the DateTime::add method to add the hours, minutes and seconds to our date + weekdays:
$weekdays = new DateTime('6 weekdays');
$weekdays->add($hms);
echo $weekdays->format('Y-m-d H:i:s'), PHP_EOL;
And you're there.
Alternatively, you could just use the basic same trick to compute the actual day-difference between your initial date, and that date + x weekdays, and then add that diff to your initial date. It's the same basic principle, but instead of having to create a format like PTXHXMXS, a simple PXD will do.
Working example here
I'd urge you to use the DateInterface classes, as it is more flexible, allows for type-hinting to be used and makes dealing with dates just a whole lot easier for all of us. Besides, it's not too different from your current code:
$today = new DateTime;
$tomorrow = new DateTime('tomorrow');
$dayAfter = new DateTime('2 days');
In fact, it's a lot easier if you want to do frequent date manipulations on a single date:
$date = new DateTime();//or DateTime::createFromFormat('Y-m-d H:i:s', $dateString);
$diff = new DateInterval('P2D');//2 days
$date->add($diff);
echo $date->format('Y-m-d H:i:s'), PHP_EOL, 'is the date + 2 days', PHP_EOL;
$date->sub($diff);
echo $date->format('Y-m-d H:i:s'), PHP_EOL, 'was the original date, now restored';
Easy, once you've spent some time browsing through the docs
I think I have found a solution. It's primitive but after some quick testing it seems to work.
The function calculates the time passed since midnight of the current day, and adds it onto the date returned by strtotime. Since this could fall into a weekend day, I've checked and added an extra day or two accordingly.
function weekDays($days) {
$tstamp = (strtotime($days.' weekdays') + (time() - strtotime("today")));
if(date('D',$tstamp) == 'Sat') {
$tstamp = $tstamp + 86400*2;
}
elseif(date('D',$tstamp) == 'Sun') {
$tstamp = $tstamp + 86400;
}
return $tstamp;
}
Function strtotime('2 weekdays') seems to add 2 weekdays to the current date without the time.
If you want to add 48 hours why not adding 2*24*60*60 to mktime()?
echo(date('Y-m-d', mktime()+2*24*60*60));
The currently accepted solution works, but it will fail when you want to add weekdays to a timestamp that is not now. Here's a simpler snippet that will work for any given point in time:
$start = new DateTime('2021-09-29 15:12:10');
$start->add(date_interval_create_from_date_string('+ 3 weekdays'));
echo $start->format('Y-m-d H:i:s'); // 2021-10-04 15:12:10
Note that this will also work for a negative amount of weekdays:
$start = new DateTime('2021-09-29 15:12:10');
$start->add(date_interval_create_from_date_string('- 3 weekdays'));
echo $start->format('Y-m-d H:i:s'); // 2021-09-24 15:12:10

knowing timezones native offset (when DST is not active) in php

I want to list for the user, all timezones with their native UTC/GMT offset, regardless of DST
How can I do it?
I 've come up with this function to do the job:
function standard_tz_offset($timezone) {
$now = new DateTime('now', $timezone);
$year = $now->format('Y');
$startOfYear = new DateTime('1/1/'.$year, $timezone);
$startOfNext = new DateTime('1/1/'.($year + 1), $timezone);
$transitions = $timezone->getTransitions($startOfYear->getTimestamp(),
$startOfNext->getTimestamp());
foreach($transitions as $transition) {
if(!$transition['isdst']) {
return $transition['offset'];
}
}
return false;
}
How it works
The function accepts a timezone and creates two DateTime objects: January 1st 00:00 of the current year and January 1st 00:00 of the next year, both specified in that timezone.
It then calculates the DST transitions during this year, and returns the offset for the first transition it finds where DST is not active.
PHP 5.3 is required because of the call to DateTimeZone::getTransitions with three parameters. If you want this to work in earlier versions you will have to accept a performance hit, because a whole lot of transitions will be generated by PHP (in this case, you don't need to bother with creating the $startOfYear and $startOfNext dates).
I have also tested this with timezones that do not observe DST (e.g. Asia/Calcutta) and it works for those as well.
To test it:
$timezone = new DateTimeZone("Europe/Athens");
echo standard_tz_offset($timezone);

How to tell if a timezone observes daylight saving at any time of the year?

In PHP, you can tell if a given date is during the Daylight Savings Time period by using something like this:
$isDST = date("I", $myDate); // 1 or 0
The problem is that this only tells you whether that one point in time is in DST. Is there a reliable way to check whether DST is in effect at any time in that timezone?
Edit to clarify:
Brisbane, Australia does not observe daylight savings at any time of the year. All year around, it is GMT+10.
Sydney, Australia does, from October to March when it changes from GMT+10 to GMT+11.
I'm wondering if there would be some existing method, or a way to implement a method which works as such:
timezoneDoesDST('Australia/Brisbane'); // false
timezoneDoesDST('Australia/Sydney'); // true
I've found a method which works using PHP's DateTimezone class (PHP 5.2+)
function timezoneDoesDST($tzId) {
$tz = new DateTimeZone($tzId);
$trans = $tz->getTransitions();
return ((count($trans) && $trans[count($trans) - 1]['ts'] > time()));
}
or, if you're running PHP 5.3+
function timezoneDoesDST($tzId) {
$tz = new DateTimeZone($tzId);
return count($tz->getTransitions(time())) > 0;
}
The getTransitions() function gives you information about each time the offset changes for a timezone. This includes historical data (Brisbane had daylight savings in 1916.. who knew?), so this function checks if there's an offset change in the future or not.
Actually nickf method didn't works for me so I reworked it a little ...
/**
* Finds wherever a TZ is experimenting dst or not
* #author hertzel Armengol <emudojo # gmail.com>
* #params string TimeZone -> US/Pacific for example
*
*/
function timezoneExhibitsDST($tzId) {
$tz = new DateTimeZone($tzId);
$date = new DateTime("now",$tz);
$trans = $tz->getTransitions();
foreach ($trans as $k => $t)
if ($t["ts"] > $date->format('U')) {
return $trans[$k-1]['isdst'];
}
}
// Usage
var_dump(timezoneExhibitsDST("US/Pacific")); --> prints false
var_dump(timezoneExhibitsDST("Europe/London")); --> prints false
var_dump(timezoneExhibitsDST("America/Chicago")); --> prints false
same function call will return true in 1 month (March) hope it helps
DateTimeZone::getTransitions might help.
You could probably wing it:
$hasDst = date("I", strtotime('June 1')) !== date("I", strtotime('Jan 1'));
Otherwise you'd need to parse the text-based zoneinfo data files.
I don't think so, but since almost every country that observes DST changes its time for an entire season or two, you could try to test 4 points during any given year.
For example, test date("I", $date) for 2009/01/01, 2009/04/01, 2009/07/01 and 2009/10/01. If that timezone falls into DST, then at least one of those dates will return 1.
date has to be on the user/server timezone for it to work, and you can't use a range with date as you do with getTransitions

Categories