calculating date diff

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

lose the single quotes around your variables.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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..

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' );
 
?>
that should allow for lower digits on year..


sending the dates to the function correctly, helps too. :)
juice
Forum Newbie
Posts: 12
Joined: Tue Feb 01, 2005 9:50 am

Post by juice »

the output for year is 2005 and not 05. sorry if confused you. ill try out loosing quotes but i think i tried that already
juice
Forum Newbie
Posts: 12
Joined: Tue Feb 01, 2005 9:50 am

Post by juice »

that worked a treat so it did.thank you. but have 1 problem but wont post it now. have enough for tonite.ill try and figure it out 2morow
Post Reply