dates
| From: | Phil Daintree | Date: | Mon, 19 Feb 2001 20:22:30 +0000 |
| Subject: | dates | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-40625@lists.php.net to get a copy of this message | ||
This is a function I use:
function DateDiff ($Date1, $Date2, $Period) {
/* expects dates in the format d/m/y - period can be one of
'd','w','y','m'
months are assumed to be 30 days and years 365.25 days This only works
provided that both dates are after 1970. Also only works for dates up to the year 2035 ish
*/
$Date1 = trim($Date1);
$Date2 = trim($Date2);
$Date1_array = explode("/", $Date1);
$Date2_array = explode("/", $Date2);
$Date1_Stamp = mktime(0,0,0,
(int)$Date1_array[1],(int)$Date1_array[0],(int)$Date1_array[2]);
$Date2_Stamp = mktime(0,0,0,
(int)$Date2_array[1],(int)$Date2_array[0],(int)$Date2_array[2]);
$Difference = $Date1_Stamp - $Date2_Stamp;
/* Difference is the number of seconds between each date negative if Date 2 > Date 1 */
switch ($Period) {
case "d":
Return (int) ($Difference/(24*60*60));
break;
case "w":
Return (int) ($Difference/(24*60*60*7));
break;
case "m":
Return (int) ($Difference/(24*60*60*30));
break;
case "s":
Return $Difference;
break;
case "y":
Return (int) ($Difference/(24*60*60*365.25));
break;
default:
Return 0;
}
}
Phil Daintree