note 110068 deleted from function.date-diff by googleguy
| From: | googleguy@php.net | Date: | Sun, 16 Sep 2012 03:38:47 +0000 |
| Subject: | note 110068 deleted from function.date-diff by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190804@lists.php.net to get a copy of this message | ||
Note Submitter: john-mackay at live dot co dot uk
----
I found this simple method works best...
// The Start and End Dates
$FirstDate = "01-01-2012" // Use "NOW" for today
$SecondDate = "01-01-2012" // Use "NOW" for today
// The Code to get the true age in days...
// Replace "NOW" with the date if stated...
if ($FirstDate=="NOW")
{
$FirstDate=date('d-m-Y')
}
if ($SecondDate=="NOW")
{
$SecondDate=date('d-m-Y')
}
//Get the Time between the two dates provided...
$ta = strtotime(FirstDate)/60/60/24;
$tb = strtotime(SecondDate)/60/60/24;
//Calculate the Difference between the two times...
$age = $ta-$tb;
//output to screen
echo $age." Day";
//adds a s to day to make days if output is 2 or more
if($age>=2){ echo "s"; }
//adds a s to day to make days if output is -2 or less
if($age<=-2){ echo "s"; }
//$age is the number of days between the FirstDate and SecondDate
Example 1...
$FirstDate = "01-01-2012"
$SecondDate = "03-01-2012"
Output = 2 Days
Example 2...
$FirstDate = "03-01-2012"
$SecondDate = "01-01-2012"
Output = -2 Days
*** I hope this helps people that like me needed a negative output as well as positive ***