Is This a PHP Date() Bug, or Is There Something Wrong with My Code

Is this a PHP date() bug, or is there something wrong with my code?

It's how dates work and when you encounter February and the 29th day of the month or later. When you add a month to a date after the last day of February of that year (i.e. 28th of February this year) you will skip over February. Whenever iterating through months you should always work with the start of the month to avoid February being skipped. So if you start with January 30th and add "one month" since there is no Feb 30th you will skip ahead to March.

Here's how you can iterate through months without knowing how many days are in February (or caring). I picked an arbitrary end date of one year from now.

$start    = new DateTimeImmutable('@'.mktime(0, 0, 0, $month_index, 1, 2014));
$end = $start->modify('+1 year')
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
echo $dt->format('F Y');
}

php date possible bug

You need to put an "of" between first monday and the date
date('Y-m-d', strtotime('first monday of 2020-06 '));
You can read about it at relative Formats

Is this a php bug, or is something wrong with my timezone/PHP settings?

My bet is on a PHP bug. I know that PHP used to have issues with catching exceptions thrown in constructors, and that seems to be what's going on here.

Because it's a bug, your options are

  1. Update the server's PHP version; you said this isn't an option
  2. Use strtotime() to find the date or, if you need or want access to the object-oriented style of DateTime, catch the error with strtotime()

For example:

<?php

try {

if (@strtotime($str) !== false) {
$x = new DateTime($str);
} else {
throw new Exception("Failed to parse string ({$str})");
}

} catch ( Exception $e ) {
echo $e->getMessage();
}

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

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;
}

PHP Date function not working properly

Solved by using

http://derickrethans.nl/obtaining-the-next-month-in-php.html

Code:

echo date('d-m-Y',strtotime("first day of next month"));

As I only needed the month number.

PHP date function error

Congratulations, you have encountered the Y2K38 problem. You will either need to switch to a 64bit system, or use the DateTime class, which can represent (virtually) arbitrary dates.

PHP Date() timestamp calculation error

check your php.ini what is the default timezone you have set for it. By default php.ini setting is UTC. Set to your timezone, and restart your web server. You should able to get the correct result. This one is by global.

Another way is in your php file, set the timezone by project. http://php.net/manual/en/function.ini-set.php

Here is your input data with this https://www.epochconverter.com/ screenshot:
Sample Image

PHP date function for week error

You need to wrap your date variable in strtotime(), the second argument for the date() function accepts a Unix timestamp which is why you're getting 1969/1970 etc - when incorrect formats are passed into date(), it defaults to the Unix epoch which is Jan 1 1970.

$currentDay = strtotime("2014-02-25");


Related Topics



Leave a reply



Submit