How to calculate the difference between two dates (and times) using Php:
Calculate Date Difference - Option 1-----------------------------------------------------
Code:
<?php
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";
?>
"If you want to get the difference between to dates or time values, then you first need to get the values in the same format. The best way is to convert both dates into Unix timestamp format. In this case the date and time information is stored as an integer so we can calculate the difference easily."See
PHP date difference for details.
Calculate Date & Time Difference - Option 2-----------------------------------------------------
"This function will find the difference between two given times. Does PHP already have a native function for that?"Code:
//The difference between 2 given time
function dateDifference($day_1,$day_2) {
$diff = strtotime($day_1) - strtotime($day_2);
$sec = $diff % 60;
$diff = intval($diff / 60);
$min = $diff % 60;
$diff = intval($diff / 60);
$hours = $diff % 24;
$days = intval($diff / 24);
return array($sec,$min,$hours,$days);
}
//Get the difference between days only:
$diff = strtotime($day_1) - strtotime($day_2) + 1; //Find the number of seconds
$day_difference = ceil($diff / (60*60*24)) ; //Find how many days that is
Posted by Binny V A to txt.binnyva.com. See
Find Difference Between 2 Dates in PHP for details.
Calculate Date Difference - Option 3-----------------------------------------------------
Code:
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
$date1="07/11/2003";
$date2="09/04/2004";
print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";
Posted by Amrit Hallan to developertutorials.com in the Php tutorials section. See
Calculating Difference Between Two Dates Using PHP for details.
Calculate Date Difference - Option 4-----------------------------------------------------
If you do not want or you cannot use Zend Framework or Pear package try this:
Code:
function dateDifference($date1, $date2)
{
$d1 = (is_string($date1) ? strtotime($date1) : $date1);
$d2 = (is_string($date2) ? strtotime($date2) : $date2);
$diff_secs = abs($d1 - $d2);
$base_year = min(date("Y", $d1), date("Y", $d2));
$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
return array
(
"years" => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
"months" => date("n", $diff) - 1,
"days_total" => floor($diff_secs / (3600 * 24)),
"days" => date("j", $diff) - 1,
"hours_total" => floor($diff_secs / 3600),
"hours" => date("G", $diff),
"minutes_total" => floor($diff_secs / 60),
"minutes" => (int) date("i", $diff),
"seconds_total" => $diff_secs,
"seconds" => (int) date("s", $diff)
);
}
Posted by Irmantas to Stackoverflow.com (See
PHP date calculation discussion for details.)
Calculate Date Difference - Option 5-----------------------------------------------------
Using the
Zend_Date Class:
Code:
// example printing difference in days
require('Zend/Date.php');
$date1 = new Zend_Date();
$date1->set(2, Zend_Date::MONTH);
$date1->set(27, Zend_Date::DAY);
$date1->set(2008, Zend_Date::YEAR);
$date2 = new Zend_Date();
$date2->set(3, Zend_Date::MONTH);
$date2->set(3, Zend_Date::DAY);
$date2->set(2008, Zend_Date::YEAR);
echo ($date2->getTimestamp() - $date1->getTimestamp()) / (3600*24);
Posted to StackOverflow.com by Ole J. Helgesen. See
PHP Zend date calculation discussion for details.
Calculate Date Difference - Option 6-----------------------------------------------------
Code:
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
$date1="07/11/2003";$date2="09/04/2004";
print "If we subtract " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";
Posted by pradeep to go4expert.com. See
Calculating difference between two dates using PHP for details.
Calculate Date Difference - Option 7 (variation of Option 6 above)
-----------------------------------------------------
Code:
function daysDifference($endDate, $beginDate)
{
//explode the date by "-" and storing to array
$date_parts1=explode("-", $beginDate);
$date_parts2=explode("-", $endDate);
//gregoriantojd() Converts a Gregorian date to Julian Day Count
$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
return $end_date - $start_date;
}
//Usage:
<?php
echo daysDifference('2008-03-12','2008-03-09');
?>
Posted by Roshan on roshanbh.com.np. See
Finding difference of days between two dates in PHP for details.
-----------------------------------------------------
Related:Php.net Date/Time FunctionsDate CalculatorThis is a simple script to calculate the difference between two dates and to express it in years, months and days. Feel free to use this script for whatever you want.Date Calculator"Date Calculator is a PHP script that allows you to calculate a date by adding or subtracting a certain number of days. This small php script was tested with apache, Linux, and Windows based systems."PHP Class for Date Time calculations"Performing data/time calculation is basic requirement in any programming project. No serious application can hide from it, so thankfully PHP itself comes with some very useful data/time functions."