×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

#### Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!
• Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# PHP FAQ

 Forum Search FAQs Links MVPs

## Coding

 Calculating the difference between dates in years, months, and days by sleipnir214 faq434-3493 Posted: 24 Apr 03 year_value,            'month' => month_value.            'day' => day_value)        The first input array is the earlier date, the second the later date.  It    will check to see that the two dates are well-formed, and that the first    date is earlier than the second.        If the function can successfully calculate the difference, it will return    an array of the form:    array (    'years' => number_of_years_different,            'months' => number_of_months_different,            'days' => number_of_days_different)                If the function cannot calculate the difference, it will return FALSE.    */function date_difference ($first,$second){    $month_lengths = array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);$retval = FALSE;    if (    checkdate($first['month'],$first['day'], $first['year']) && checkdate($second['month'], $second['day'],$second['year'])        )    {        $start = smoothdate ($first['year'], $first['month'],$first['day']);        $target = smoothdate ($second['year'], $second['month'],$second['day']);                                    if ($start <=$target)        {            $add_year = 0; while (smoothdate ($first['year']+ 1, $first['month'],$first['day']) <= $target) {$add_year++;                $first['year']++; }$add_month = 0;            while (smoothdate ($first['year'],$first['month'] + 1, $first['day']) <=$target)            {                $add_month++;$first['month']++;                                if ($first['month'] > 12) {$first['year']++;                    $first['month'] = 1; } }$add_day = 0;            while (smoothdate ($first['year'],$first['month'], $first['day'] + 1) <=$target)            {                if (($first['year'] % 100 == 0) && ($first['year'] % 400 == 0))                {                    $month_lengths = 29; } else { if ($first['year'] % 4 == 0)                    {                        $month_lengths = 29; } }$add_day++;                $first['day']++; if ($first['day'] > $month_lengths[$first['month'] - 1])                {                    $first['month']++;$first['day'] = 1;                                        if ($first['month'] > 12) {$first['month'] = 1;                    }                }                            }                                                                                                                                                                                                                                                                    $retval = array ('years' =>$add_year, 'months' => $add_month, 'days' =>$add_day);        }    }                                                                                                                                                                                                                                                                                    return $retval;}/* This code is merely an example of use of the function*/print ' ';$begin = array ('year' => 2001, 'month' => 3, 'day' => 14);$end = array ('year' => 2004, 'month' => 3, 'day' => 14);$foo = date_difference ($begin,$end);if ($foo !== FALSE){ print_r ($foo);}else{    print 'FALSE';}?>
Back to PHP FAQ Index 			Back to PHP Forum

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.