note 40960 deleted from function.mktime by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 07 Nov 2004 20:28:58 +0000 |
| Subject: | note 40960 deleted from function.mktime by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80247@lists.php.net to get a copy of this message | ||
Note Submitter: dzyga at adoptassoc dot com
----
I noticed that most date-difference functions here are very complex and too powerful.
Here's a simple one for simple-minded folk like myself that just returns the difference (in
days) between two dates:
#########################
# date_diff Function
#########################
#
# Calculate the difference between 2 dates in YYYY-MM-DD format.
# Returns the number of days difference.
#
function date_diff($date1, $date2)
# Note that $date2 must be later than $date1
{
# Get the year, month, day values of the two dates
$d1 = explode("-", $date1);
$d2 = explode("-", $date2);
# Construct UNIX timestamps based on the original dates
$date1 = mktime(0,0,0, date($d1[1]), date($d1[2]), date($d1[0]));
$date2 = mktime(0,0,0, date($d2[1]), date($d2[2]), date($d2[0]));
$tmp = $date1;
$y = $d1[0];
$m = $d1[1];
$d = $d1[2];
$days = 0;
# Increments $tmp one day at a time until it matches $date2
while($tmp != $date2)
{
$d++;
$tmp = mktime(0,0,0, date($m), date($d), date($y));
$days++;
} # while
# Returns the number of increments for the dates to match
return $days;
} # date_diff()
?>
Hope this helps solve the basic problems.