I've managed to reduce this to a small script which reproduces the issue (Tried on two different PC's, but both with 5.3.6):
<?php
$item = array('monthly_on_the' => 4);
$date = new DateTime();
$date->modify('first day of this month');
print_r($date);
$interval = new DateInterval('P'.$item['monthly_on_the'].'D');
print_r($interval);
$return = $date->add($interval);
if (!$return) die('Bad stuff happened!');
print_r($date);
DateTime Object
(
[date] => 2012-02-01 17:15:23
[timezone_type] => 3
[timezone] => Australia/Sydney
)
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 4
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] =>
)
DateTime Object
(
[date] => 2012-02-01 17:15:23
[timezone_type] => 3
[timezone] => Australia/Sydney
)
Is this a bug and if so, has it been fixed in more recent versions of PHP? Or is there something weird in the time stuff that I'm overlooking?
Yes it's a bug. I'm experiencing the same thing with PHP 5.3.6. Apparently it's fixed in 5.3.7. See the changelog:
http://www.php.net/ChangeLog-5.php#5.3.7
Bug Report:
https://bugs.php.net/bug.php?id=54340
Workaround (if you can't upgrade):
$date->setTimestamp(strtotime('first day of this month', $date->getTimestamp()));
Related
I have problem...
I'm currently working with "America/Santiago" timezone, and whenever I create a datetime object with a hour < 01:00AM, it adds 1 hour.
Example:
date_default_timezone_set('America/Santiago');
$dateTest = date_create_from_format('d/m/Y H:i:s', '04/09/2022 00:32:27');
print_r($dateTest);
This prints:
DateTime Object
(
[date] => 2022-09-04 01:32:27.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
But if I create the following object:
date_default_timezone_set('America/Santiago');
$dateTest = date_create_from_format('d/m/Y H:i:s', '04/09/2022 01:22:11');
print_r($dateTest);
prints:
DateTime Object
(
[date] => 2022-09-04 01:22:11.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
I'm really lost here, can someone guide ?
Thanks
I met an interesting case, related to the diff() method of DateTime class.
If I try to calculate difference between two dates in months like
$datetime1 = new \DateTime('June 2019');
$datetime2 = new \DateTime('July 2019');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%m');
, as result I get 0.
Why does this happen?
Print_r's:
$datetime1:
DateTime Object ( [date] => 2019-06-01 00:00:00.000000
[timezone_type] => 3 [timezone] => Europe/Berlin )
$datetime2:
DateTime Object ( [date] => 2019-07-01 00:00:00.000000
[timezone_type] => 3 [timezone] => Europe/Berlin )
$interval:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 30 [h] => 0 [i] => 0 [s] => 0 [f] => 0
[weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0
[invert] => 0 [days] => 30 [special_type] => 0 [special_amount] => 0
[have_weekday_relative] => 0 [have_special_relative] => 0 )
There is big inconsistency with timezone and date handing in PHP
This appears to be a bug (in so far as the datetime format is forced to a GMT* offset, according to this comment).
*(but forcing to GMT seems inconsistent with the results established by the code below)
Setting the server timezone value to any timezone does not effect this script timezone anomaly.
Below are two cases showing what happens in different time zones:
Case 1:
The following code will output a list of results for each time zone:
$tzList = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
print "Current Zone:". print_r(ini_get('date.timezone'),true)."<br>\n<BR>\n";
foreach($tzList as $tzRow) {
$tz = new DateTimeZone($tzRow);
//$tz = null;
$datetime1 = new \DateTime('June 2019', $tz);
$datetime2 = new \DateTime('July 2019', $tz);
$interval = $datetime1->diff($datetime2, false);
echo $interval->format('%a %m') . PHP_EOL. " :: ";
print print_r($datetime1->getTimezone(),true)."<BR>";
}
The result of this list output shows a high (~60%) rate of 0 and the rest of 1 month .
Please see here: http://sandbox.onlinephpfunctions.com/code/b18ba13deb94d112b12630a12265363fb6c7670b
Case 2:
Setting the timezone AFTER creating the object, results in a consistent answer (albeit incorrect)
$tzList = DateTimeZone::listIdentifiers(DateTimeZone::ALL);
print "Current Zone:". print_r(ini_get('date.timezone'),true)."<br>\n<BR>\n";
foreach($tzList as $tzRow) {
//$tz = new DateTimeZone($tzRow);
$tz = null;
$datetime1 = new \DateTime('June 2019', $tz);
$datetime2 = new \DateTime('July 2019', $tz);
$datetime1->setTimezone(new DateTimeZone($tzRow));
$datetime2->setTimezone(new DateTimeZone($tzRow));
$interval = $datetime1->diff($datetime2, false);
echo $interval->format('%a %m') . PHP_EOL. " :: ";
print print_r($datetime1->getTimezone(),true)."<BR>";
}
This output's generated here all all 30 days out; but all 0 months difference.
See code here: http://sandbox.onlinephpfunctions.com/code/7bcc62f4e36f41df71b9cb928de75a53f233d9fd
So it's your choice if you want to use sometimes correct results or universally incorrect rbut consistent results, by setting when you establish the Timezone value in the DateTime objects.
Possible Solution:
If the server timezone is correctly set to UTC "correct" timezone (that naturally returns "1" month in Case 1, then CASE 2 above works consistently across all time zones given to the DateTime objects.
The problem is in your timezone.
There is a post explaining about it here.
See this example:
<?php
echo "----- Europe/Berlin -----\n";
date_default_timezone_set('Europe/Berlin');
$datetime1 = new \DateTime('June 2019');
$datetime2 = new \DateTime('July 2019');
print_r($datetime1);
print_r($datetime2);
$interval = $datetime1->diff($datetime2);
print_r($interval);
echo "%m = " . $interval->format('%m') . PHP_EOL;
echo "%a = " . $interval->format('%a') . PHP_EOL;
echo "%s = " . $interval->format('%s') . PHP_EOL;
echo "\n\n\n----- America/Sao_Paulo -----\n";
date_default_timezone_set('America/Sao_Paulo');
$datetime1 = new \DateTime('June 2019');
$datetime2 = new \DateTime('July 2019');
print_r($datetime1);
print_r($datetime2);
$interval = $datetime1->diff($datetime2);
print_r($interval);
echo "%m = " . $interval->format('%m') . PHP_EOL;
echo "%a = " . $interval->format('%a') . PHP_EOL;
echo "%s = " . $interval->format('%s') . PHP_EOL;
And the output:
$ php date_diff.php
----- Europe/Berlin -----
DateTime Object
(
[date] => 2019-06-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
DateTime Object
(
[date] => 2019-07-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 30
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 30
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
%m = 0
%a = 30
%s = 0
----- America/Sao_Paulo -----
DateTime Object
(
[date] => 2019-06-01 00:00:00.000000
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
DateTime Object
(
[date] => 2019-07-01 00:00:00.000000
[timezone_type] => 3
[timezone] => America/Sao_Paulo
)
DateInterval Object
(
[y] => 0
[m] => 1
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 30
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
%m = 1
%a = 30
%s = 0
In my timezone $interval->format('%m'); is 1.
You can set timezone on your dates to calculate the difference between them.
$datetime1 = new \DateTime('June 2019', new DateTimeZone('UTC'));
$datetime2 = new \DateTime('July 2019', new DateTimeZone('UTC'));
$interval = $datetime1->diff($datetime2);
print_r($interval);
echo "%m = " . $interval->format('%m') . PHP_EOL;
$ php date_diff.php
DateInterval Object
(
[y] => 0
[m] => 1
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 30
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
%m = 1
Can you try it by adding a timezone?
$timezones = [
'UTC',
'Europe/Berlin',
'America/Belize',
'Asia/Hong_Kong',
];
foreach ($timezones as $timezone) {
$tz = new DateTimeZone($timezone);
$datetime1 = new \DateTime('June 2019', $tz);
$datetime2 = new \DateTime('July 2019', $tz);
$interval = $datetime1->diff($datetime2);
echo str_pad($timezone, 20, ' ').' '.$interval->format('months: %M, day: %D, days: %a') . PHP_EOL;
}
Result:
UTC months: 01, day: 00, days: 30
Europe/Berlin months: 00, day: 30, days: 30
America/Belize months: 01, day: 00, days: 30
Asia/Hong_Kong months: 00, day: 30, days: 30
I would like to get the date time interval between two dates in a time zone that uses daylight savings time.
The following snipet shows the problem.
$timezone = new DateTimeZone('UTC');
$from_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-10-31 23:59:59',$timezone);
$to_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-11-20 23:47:02',$timezone);
$interval = $from_date_obj->diff($to_date_obj, TRUE);
print_r($interval);
For UTC timezone this shows:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 19
[h] => 23
[i] => 47
[s] => 3
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 19
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
Now this one:
$timezone = new DateTimeZone('America/Los_Angeles');
$from_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-10-31 23:59:59',$timezone);
$to_date_obj = DateTime::createFromFormat('Y-m-d H:i:s', '2014-11-20 23:47:02',$timezone);
$interval = $from_date_obj->diff($to_date_obj, TRUE);
print_r($interval);
shows:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 20
[h] => -1
[i] => 47
[s] => 3
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 19
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
the one in UTC shows 19 days/23 hours/47 minutes and 3 seconds. The one for PT shows 20 days/-1 hours/47 minutes and 3 seconds. I think the right answer for PT should be 20 days/0 hours/47 minutes and 3 seconds.
Is there any way to do a diff in the pacific time zone and not end up with the negative hour? Is the -1 hours indication the desired result and what does it mean exactly?
update: I found out that indeed PHP doesn't seem to handle the diff with respect to DST which is discussed here:
PHP's DateTime::Diff gets it wrong?
As far as the results I was getting it appears to be a PHP bug as indicated in the above link.
There are two issues raised here:
One has to do with the difference between two dates and times when there is a clock change due to DST (Daylight Savings Time). In reality the difference between two dates and times should take into account the clock change, but apparently it does not.
That is documented pretty well in the following link:
PHP's DateTime::Diff gets it wrong?
Further I am chalking up the -1 problem as a php bug. Since PHP doesn't handle the DST changes between two date times anyway, and in my case the difference without taking DST into effect is close enough anyway, I will just do the diff calculation in UTC.
What php version are you using ? Im getting this result for 2nd code block -
DateInterval Object (
[y] => 0
[m] => 0
[d] => 19
[h] => 23
[i] => 47
[s] => 3
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 19
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
both of the codes should display the same result as your comparing the same date with just different timezone each time
Even after 8 years it is still not supported:
$day = \DateTime::createFromFormat('Y-m-d', '2023-03-26', new \DateTimeZone('Europe/Budapest'));
$followingDay = \DateTime::createFromFormat('Y-m-d', $day->format('Y-m-d'), new \DateTimeZone('Europe/Budapest'));
$followingDay->add(new \DateInterval('P1D'));
$day2 = \DateTime::createFromFormat('Y-m-d', '2023-03-27', new \DateTimeZone('Europe/Budapest'));
var_dump($day->getTimestamp(), $followingDay->getTimeStamp(), $day2->getTimeStamp());
var_dump($day, $followingDay, $day2);
Not to mention that it adds current time instead of 00:00:00 which I read in the documentation, so better to be careful. Though I generally hate PHP because of the low quality documentation, so I almost always write automated tests when I use a built-in function. It is full of bugs or undocumented behavior.
int(1679794696)
int(1679881096)
int(1679877496)
class DateTime#397 (3) {
public $date =>
string(26) "2023-03-26 03:38:16.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(15) "Europe/Budapest"
}
class DateTime#404 (3) {
public $date =>
string(26) "2023-03-27 03:38:16.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(15) "Europe/Budapest"
}
class DateTime#399 (3) {
public $date =>
string(26) "2023-03-27 02:38:16.000000"
public $timezone_type =>
int(3)
public $timezone =>
string(15) "Europe/Budapest"
}
$start = new DateTime('2014-01-07', new DateTimeZone('UTC'));
$end = clone $start;
$start->sub(new DateInterval('P1M')); // substract one month
echo $start->format('Y-m-d').' - '.$end->format('Y-m-d');
// 2013-12-07 - 2014-01-07 - seems correct, let's get number of days...
print_r($end->diff($start));
// DateInterval Object ( [y] => 0 [m] => 2 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [invert] => 1 [days] => 61 )
61 days??? What is going on in here?
try this,
$diff=date_diff($date1,$date2);
How do you get today's date, as a date object?
I'm trying to compute the difference between some start date and today. The following will not work, because getdate() returns an array and not a date object:
$today = getdate();
$start = date_create('06/20/2012');
$diff = date_diff($start, $today);
echo($today . '<br/>' . $start . '<br/>' . $diff);
Output:
Array ( [seconds] => 8 [minutes] => 1 [hours] => 16 [mday] => 11 [wday] => 1 [mon] => 6 [year] => 2012 [yday] => 162 [weekday] => Monday [month] => June [0] => 1339455668 )
DateTime Object ( [date] => 2012-06-20 00:00:00 [timezone_type] => 3 [timezone] => America/Los_Angeles )
new DateTime('now');
http://www.php.net/manual/en/datetime.construct.php
Comparing is easy:
$today = new DateTime('now');
$newYear = new DateTime('2012-01-01');
if ($today > $newYear) {
}
Op's edit
I just needed to call date_default_timezone_set, and then this code worked for me.
To get difference in days use this:
$today = new DateTime('today');
the time in this object eill be 00:00:00
If you want difference with hours minutes and seconds use this:
$now = new DateTime('now');
I ended up using the date_create constructor (no parameter) to get the current date.
$diff = date_diff(date_create('06/20/2012'), date_create());
print_r($diff);
Output:
DateInterval Object ( [y] => 0 [m] => 0 [d] => 8 [h] => 6 [i] => 30 [s] => 40 [invert] => 1 [days] => 8 )
I have no idea why, but Mike B's answer (and any constructor I tried for DateTime) threw an error for me in PHP5 / IIS.