How do I calculate the difference between two dates in hours?
For example:
day1=2006-04-12 12:30:00
day2=2006-04-14 11:30:00
In thi
<?
$day1 = "2014-01-26 11:30:00";
$day1 = strtotime($day1);
$day2 = "2014-01-26 12:30:00";
$day2 = strtotime($day2);
$diffHours = round(($day2 - $day1) / 3600);
echo $diffHours;
?>
This function helps you to calculate exact years and months between two given dates, $doj1
and $doj
. It returns example 4.3 means 4 years and 3 month.
<?php
function cal_exp($doj1)
{
$doj1=strtotime($doj1);
$doj=date("m/d/Y",$doj1); //till date or any given date
$now=date("m/d/Y");
//$b=strtotime($b1);
//echo $c=$b1-$a2;
//echo date("Y-m-d H:i:s",$c);
$year=date("Y");
//$chk_leap=is_leapyear($year);
//$year_diff=365.25;
$x=explode("/",$doj);
$y1=explode("/",$now);
$yy=$x[2];
$mm=$x[0];
$dd=$x[1];
$yy1=$y1[2];
$mm1=$y1[0];
$dd1=$y1[1];
$mn=0;
$mn1=0;
$ye=0;
if($mm1>$mm)
{
$mn=$mm1-$mm;
if($dd1<$dd)
{
$mn=$mn-1;
}
$ye=$yy1-$yy;
}
else if($mm1<$mm)
{
$mn=12-$mm;
//$mn=$mn;
if($mm!=1)
{
$mn1=$mm1-1;
}
$mn+=$mn1;
if($dd1>$dd)
{
$mn+=1;
}
$yy=$yy+1;
$ye=$yy1-$yy;
}
else
{
$ye=$yy1-$yy;
$ye=$ye-1;
$mn=12-1;
if($dd1>$dd)
{
$ye+=1;
$mn=0;
}
}
$to=$ye." year and ".$mn." months";
return $ye.".".$mn;
/*return daysDiff($x[2],$x[0],$x[1]);
$days=dateDiff("/",$now,$doj)/$year_diff;
$days_exp=explode(".",$days);
return $years_exp=$days; //number of years exp*/
}
?>
The newer PHP-Versions provide some new classes called DateTime
, DateInterval
, DateTimeZone
and DatePeriod
. The cool thing about this classes is, that it considers different timezones, leap years, leap seconds, summertime, etc. And on top of that it's very easy to use. Here's what you want with the help of this objects:
// Create two new DateTime-objects...
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');
// The diff-methods returns a new DateInterval-object...
$diff = $date2->diff($date1);
// Call the format method on the DateInterval-object
echo $diff->format('%a Day and %h hours');
The DateInterval-object, which is returned also provides other methods than format
. If you want the result in hours only, you could to something like this:
$date1 = new DateTime('2006-04-12T12:30:00');
$date2 = new DateTime('2006-04-14T11:30:00');
$diff = $date2->diff($date1);
$hours = $diff->h;
$hours = $hours + ($diff->days*24);
echo $hours;
And here are the links for documentation:
All these classes also offer a procedural/functional way to operate with dates. Therefore take a look at the overview: http://php.net/manual/book.datetime.php
your answer is:
round((strtotime($day2) - strtotime($day1))/(60*60))
Carbon could also be a nice way to go.
From their website:
A simple PHP API extension for DateTime. http://carbon.nesbot.com/
Example:
use Carbon\Carbon;
//...
$day1 = Carbon::createFromFormat('Y-m-d H:i:s', '2006-04-12 12:30:00');
$day2 = Carbon::createFromFormat('Y-m-d H:i:s', '2006-04-14 11:30:00');
echo $day1->diffInHours($day2); // 47
//...
Carbon extends the DateTime class to inherit methods including diff()
. It adds nice sugars like diffInHours
, diffInMintutes
, diffInSeconds
e.t.c.
$t1 = strtotime( '2006-04-14 11:30:00' );
$t2 = strtotime( '2006-04-12 12:30:00' );
$diff = $t1 - $t2;
$hours = $diff / ( 60 * 60 );