Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Date difference

Tags:

date

php

diff

i've the following code:

$dStart = new DateTime('2013-03-15');
$dEnd = new DateTime('2013-04-01');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->days;

I don't know why i'm getting 6015 as result.

like image 453
carlosduarte Avatar asked Apr 01 '13 12:04

carlosduarte


People also ask

How can I get the difference between two dates in PHP?

$date1 = "2007-03-24"; $date2 = "2009-06-26"; $diff = abs(strtotime($date2) - strtotime($date1)); $years = floor($diff / (365*60*60*24)); $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); printf("%d years, %d months, %d ...

How do you find the date difference?

Use the DATEDIF function when you want to calculate the difference between two dates. First put a start date in a cell, and an end date in another. Then type a formula like one of the following. Warning: If the Start_date is greater than the End_date, the result will be #NUM!.

How can I get the difference between two timestamps in PHP?

To calculate the difference between two dates in PHP, call date_diff() date/time function, and pass the two dates as argument to it. date_diff() function returns a DateInterval object, or FALSE if calculating the difference is not successful.

Can you compare time in PHP?

In order to compare those two dates we use the method diff() of the first DateTime object with the second DateTime object as argument. The diff() method will return a new object of type DateInterval .


1 Answers

Try like

$dStart = strtotime('2013-03-15');
$dEnd = strtotime('2013-04-01');
$dDiff = $dEnd - $dStart;
echo date('H:i:s',$dDiff);

or as per your code try with

$dDiff = $dStart->diff($dEnd);
$date->format('d',$dDiff);
echo $dDiff->days;

if you want diff in days try with this also

echo floor($dDiff/(60*60*24));
like image 188
Gautam3164 Avatar answered Oct 03 '22 16:10

Gautam3164