I was going through a php code today and found this to be really weird.
<?php
$now = new DateTime("2015-07-29 03:38:55");
$previous = new DateTime("2013-07-29 05:06:40");
$diff = $now->diff($previous);
$diff2 = $previous->diff($now);
printf("%d years, %d month, %d days, %d hours, %d minutes %d seconds.<br/>", $diff->y, $diff->m, $diff->d, $diff->h, $diff->i, $diff->s);
printf('%d years, %d month, %d days, %d hours, %d minutes %d seconds', $diff->y, $diff->m, $diff2->d, $diff2->h, $diff2->i, $diff2->s);
The output is
1 years, 11 month, 30 days, 22 hours, 32 minutes 15 seconds
1 years, 11 month, 29 days, 22 hours, 32 minutes 15 seconds
I have two questions
I read the document it said that $previous->diff($now); shows $now - $previous. However, if it is the other way around, wouldn't it be negative?
Why is one 30 days and the other is 29 days?
I have a speculation that this could be due to the invert. I'm not entirely sure why it is adding an extra day... but a quick change I did was:
$now = new DateTime("2015-07-29 03:38:55");
$previous = new DateTime("2013-07-29 05:06:40");
$diff = $now->diff($previous);
$diff2 = $previous->diff($now);
printf("%d years, %d month, %d days, %d hours, %d minutes %d seconds\r\n", $diff->y, $diff->m, ($diff->d - $diff->invert), $diff->h, $diff->i, $diff->s);
printf('%d years, %d month, %d days, %d hours, %d minutes %d seconds', $diff->y, $diff->m, ($diff2->d - $diff2->invert), $diff2->h, $diff2->i, $diff2->s);
And the output is:
1 years, 11 month, 29 days, 22 hours, 32 minutes 15 seconds
1 years, 11 month, 29 days, 22 hours, 32 minutes 15 seconds
Or, alternatively, the above is completely wrong and is just a coincidence... another explanation may be that the diff days was a float value for $diff, and so it was rounded down... yielding 29 days. Possible leap-year issue as well.
Edit
The above is also wrong... if you change the output to floats, it will show whole numbers. This adds to the confusion. I'll leave this answer to possibly help someone else figure out the solution.
$now = new DateTime("2015-07-29 03:38:55", new DateTimeZone('GMT'));
$previous = new DateTime("2013-07-29 05:06:40", new DateTimeZone('GMT'));
$diff = $now->diff($previous);
$diff2 = $previous->diff($now);
printf("%f years, %f month, %f days, %f hours, %f minutes %f seconds %f total days\r\n", $diff->y, $diff->m, $diff->d, $diff->h, $diff->i, $diff->s, $diff->days);
printf('%f years, %f month, %f days, %f hours, %f minutes %f seconds %f total days', $diff->y, $diff->m, $diff2->d, $diff2->h, $diff2->i, $diff2->s, $diff2->days);
I even set the timezone in hopes to help. Still yields whole numbers:
1.000000 years, 11.000000 month, 30.000000 days, 22.000000 hours, 32.000000 minutes 15.000000 seconds 729.000000 total days
1.000000 years, 11.000000 month, 29.000000 days, 22.000000 hours, 32.000000 minutes 15.000000 seconds 729.000000 total days
the sign of the difference is in the invert property: $diff->invert=1 and $diff2->invert=0
You can see in the php source here a day correction by 1 when:
the 1st of the dates is in DST and the 2nd is not
the timestamp of the 2nd date starts after the timetstamp of the 1st one without the DST correction
the timestamp of the 2nd date starts before the timetstamp of the 1st one with the DST correction
This is what is causing one to show 30 days and the other to show 29. In #Half Crazed's example, both dates are in DST, so no correction there. If the previous date of the OP were 2013-07-29 03:06:40, their wouldn't be a difference
Related
From Convert seconds into days, hours, minutes and seconds we can get the good function of User Galvic:
function secondsToTime($seconds){
$dtF = new \DateTime('#0');
$dtT = new \DateTime("#$seconds");
return $dtF -> diff($dtT) -> format('%a days, %h hours, %i minutes and %s seconds');
}
echo secondsToTime(3600 * 24 * 31);
and this work greath!, but I want add months, then I put %m months return 0, and %M months return two digits.
How to convert 3600 * 24 * 32 in [1 month and 2 day] and 3600 * 24 * 30 * 13 in [1 year and 1 month]?
Becosue in Function of Galvic exist many options, but only ONE using 2 lines: GALVIC function, then I want add to this two lines the option "months, year".
Thanks
Don't use %a for days, use %d instead.
return $dtF->diff($dtT)->format('%y years, %m months, %d days');
I have a timestamp which calculates remaining time between 2 dates.
$job_expiration = strtotime($job['job_expiration']) - time(); like so.
Right now, I'm showing only 1 day or 2 days with the date() function.
date('j', $job_expiration) like so.
I also want to show hours instead of 2 days. let's say the time remaining is 1 day 16 hours or only 30 minutes. then I want to show the hours with day too.
1 day, 16 hours or only 30 minutes
how can I do that?
function convert_seconds($seconds) {
$dt1 = new DateTime("#0");
$dt2 = new DateTime("#$seconds");
return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo convert_seconds(200000)."\n";
Sample output:
2 days, 7 hours, 33 minutes and 20 seconds
In your case you should use this function like convert_seconds($job_expiration)
I am implementing a real Estate application and I need to EXACTLY check the date difference between two dates.
I will prefer for this a javascript jquery approach, but any other solution will be welcome.
My goal is to not allow users to enter dates where the difference is EXACTLY bigger than 2 (or 5) years.
I have tried, along with many others, this solution:
How to get the months between 2 dates
Unfortunately, this is not what I really need.
When I do:
The rent will start at 01-01-2017
And will end at 31-12-2018
I will get 24 months (exactly rented for 2 years), which is correct for my purpose.
But if I try:
The rent will start at 01-01-2017
And will end at 01-01-2019
I will still get 24 months (but it is rented for 2 years and 1 day), which is not correct for my purpose.
Due to the law regulations in my country (Netherlands) there is for the contracts a big difference between the two above mentioned situations.
I will appreciate any help from your side.
As mentioned in the comment, add 2 years and subtract 1 day. Best to do it with DateTime class:
$dt = DateTime::createFromFormat('d-m-Y', '01-01-2017');
$dt->add(new DateInterval('P2Y'));
$dt->sub(new DateInterval('P1D'));
var_dump($dt->format('d-m-Y'));
This will produce the desired result:
string(10) "31-12-2018"
Here's an excellent function by SunilKmCharde, from the User Contributed Notes in the PHP manual:
<?php
//////////////////////////////////////////////////////////////////////
//PARA: Date Should In YYYY-MM-DD Format
//RESULT FORMAT:
// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days
// '%m Month %d Day' => 3 Month 14 Day
// '%d Day %h Hours' => 14 Day 11 Hours
// '%d Day' => 14 Days
// '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds
// '%i Minute %s Seconds' => 49 Minute 36 Seconds
// '%h Hours => 11 Hours
// '%a Days => 468 Days
//////////////////////////////////////////////////////////////////////
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
?>
This should be accurate and be aware of leap years etc...
<?php
$date = date_create('02-01-2017');
date_add($date, date_interval_create_from_date_string('2 years'));
$twoyears = date_format($date, 'd-m-Y');
echo $twoyears; // 02-01-2019
$rentperiod = $twoyears->sub(DateInterval::createFromDateString('1 day'));
echo $rentperiod; //01-31-2019
Javascript:
function diff( d1, d2){
x = new Date(d2-d1);
return x.getUTCDate() + " " + x.getUTCMonth() + " " + (x.getUTCFullYear() - 1970);
}
example:
a = new Date('01-01-2017');
b = new Date('01-01-2019');
diff(a,b);
"Days:1, Months: 0, Years: 2"
$end=date_create("2013-07-30 00:30:33");
$now=date_create();
$x=date_diff($end,$now);
echo $x->format('%a days');
When I use %a it returns 45 days which is correct, when I use %d it returns 15 days. What is problem there?
Number 15 are the days calculated from difference by the months.
For example: (from http://www.php.net/manual/en/dateinterval.format.php)
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
The above example will output:
31 total days
1 month, 0 days
Note that date_diff($end,$now); returns DateInterval and it has its own format:
FROM PHP DOC
a = Total number of days as a result of a DateTime::diff() or (unknown) otherwise
And
d = Days, numeric
You can not have 45 days in a single month so its basically using %d or %m month %d days
45 days //or
1 month 15 days
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP DateTime::days returns trash?
Ok, I don't get this... Could someone explain what I'm doing wrong here?
date_default_timezone_set('Europe/Oslo');
$a = new DateTime('2011-06-20 21:00:00');
$b = new DateTime('2011-06-21 05:30:00');
echo $a->format('Y-m-d H:i:s') . PHP_EOL;
echo $b->format('Y-m-d H:i:s') . PHP_EOL;
echo $a->diff($b)->format('%a days, %h hours, %i minutes and %s seconds');
echo $a->diff($b)->format('%y years, %m months, %d days, %h hours, %i minutes and %s seconds').PHP_EOL;
The output I get is:
2011-06-20 21:00:00
2011-06-21 05:30:00
6015 days, 8 hours, 30 minutes and 0 seconds
0 years, 0 months, 0 days, 8 hours, 30 minutes and 0 seconds
What is up with the days here? Shouldn't it be 0 in both cases?
It should, and it does on my system:
nanne#pustule:~$ cat test.php
<?
date_default_timezone_set('Europe/Oslo');
$a = new DateTime('2011-06-20 21:00:00');
$b = new DateTime('2011-06-21 05:30:00');
echo $a->format('Y-m-d H:i:s') . PHP_EOL;
echo $b->format('Y-m-d H:i:s') . PHP_EOL;
echo $a->diff($b)->format('%a days, %h hours, %i minutes and %s seconds');
//
nanne#pustule:~$ php test.php
2011-06-20 21:00:00
2011-06-21 05:30:00
0 days, 8 hours, 30 minutes and 0 seconds
As #pekka commented: it seems to be a bug in PHP for windows systems :D