Possible Duplicate:
How to count days between two dates in PHP?
Seems there's only a javascript version,what about in PHP?
Possible Duplicate:
How to count days between two dates in PHP?
Seems there's only a javascript version,what about in PHP?
Taken almost directly from an article I wrote a few weeks ago: Working with Date and Time in PHP
$today = new DateTime();
$ref = new DateTime("2011-05-20");
$diff = $today->diff($ref);
echo "the difference is {$diff->days} days" . PHP_EOL;
Calculate the seconds they differ, and you can easily calculate the amount of days.
$oFirstDate = new DateTime($sDateFormat);
$oSecondDate = new DateTime($sDateFormat2);
$iSeconds = $oFirstDate->getTimeStamp() - $oSecondDate->getTimeStamp();
$iDays = $iSeconds / 60 / 60 / 24;
I do agree with Shakti; with minimal changes, the script from the other question will work for you:
<?php
$datetime1 = date_create( date( 'Y-m-d' ) );
$datetime2 = date_create('2011-05-21');
$interval = date_diff($datetime1, $datetime2);
echo $interval->days . " days difference.";
Well, I have a generic-ish function for stuff like that:
function timediffIn($time, $unit, $human = False){
$tokens = array (
'years' => 31536000,
'months' => 2592000,
'weeks' => 604800,
'days' => 86400,
'hours' => 3600,
'minutes' => 60,
'seconds' => 1
);
if(!array_key_exists($unit, $tokens)){
if ($human) print "No such unit: $unit\n";
return FALSE;
}
if(!strtotime($time)){
if ($human) print "$time does not translate into a valid time\n";
return FALSE;
}
$elapsed = time() - strtotime($time);
$interval = $tokens[$unit];
if($human){
print "It has been " . floor($elapsed / $interval) . " $unit since $time\n";
}
return floor($elapsed / $interval);
}
HTH