calculating date diff
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
juice, did you get the memo about the TPS repor -- I mean -- Posting Code Guidelines?
'05' for a year is 1905 to the functions, which is an "invalid" date. You specifically said in your original post the year is coming across 2004.. oh well..that should allow for lower digits on year..
sending the dates to the function correctly, helps too.
'05' for a year is 1905 to the functions, which is an "invalid" date. You specifically said in your original post the year is coming across 2004.. oh well..
Code: Select all
<?php
function makeTimestamp( $date )
{
static $months = array( 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
$fixed = preg_replace('#^\s*(\d{1,2})\s*(ї-/\.])\s*(\d{1,2})\s*\\2\s*(\d{2,4})\s*$#e', 'sprintf(\'%02d-%s-%04d\', intval(\'\\1\'), $monthsїintval(\'\\3\')], (strlen(\'\\4\') < 4 ? (intval(\'\\4\') < 70 ? 2000 : 1900) : 0) + intval(\'\\4\') )', $date);
return strtotime($fixed);
}
function toDays( $timestamp )
{
return intval(intval($timestamp) / (24 * 60 * 60));
}
function dateDiff( $date1, $date2 )
{
$date1 = toDays(makeTimestamp( $date1 ));
$date2 = toDays(makeTimestamp( $date2 ));
return abs($date1 - $date2);
}
echo dateDiff( '1-3-2005', '05-03-2005' );
?>sending the dates to the function correctly, helps too.