note 43091 deleted from function.mktime by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 07 Nov 2004 20:28:01 +0000 |
| Subject: | note 43091 deleted from function.mktime by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80242@lists.php.net to get a copy of this message | ||
Note Submitter: gustav at monoseven dot com
----
Modified shadow2day's script. Returns the difference between two given dates in seconds,
minutes, hours or days. And now it also works :)
<?php
// Get date difference between two given dates
// $returntype: s = seconds, m = minutes, h = hours, d = days
// int date_diff(int start_date, int end_date[, string return_type])
function date_diff($start_date, $end_date, $returntype="d")
{
if ($returntype == "s")
$calc = 1;
if ($returntype == "m")
$calc = 60;
if ($returntype == "h")
$calc = (60*60);
if ($returntype == "d")
$calc = (60*60*24);
$_d1 = explode("-", $start_date);
$y1 = $_d1[0];
$m1 = $_d1[1];
$d1 = $_d1[2];
$_d2 = explode("-", $end_date);
$y2 = $_d2[0];
$m2 = $_d2[1];
$d2 = $_d2[2];
if (($y1 < 1970 || $y1 > 2037) || ($y2 < 1970 || $y2 > 2037))
{
return 0;
} else
{
$today_stamp = mktime(0,0,0,$m1,$d1,$y1);
$end_date_stamp = mktime(0,0,0,$m2,$d2,$y2);
$difference = round(($end_date_stamp-$today_stamp)/$calc);
return $difference;
}
}
echo date_diff("2004-06-6", "2004-06-07", "s");
?>