View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

Calculate age from two dates

Thread began 6/26/2009 12:11 pm by Daryl | Last modified 6/26/2009 1:57 pm by Daryl | 3805 views | 2 replies

DarylBeta Tester

I managed to solve it myself so thought I would post my solution for anyone else who might like to do this;

I created a file called datediff.php which contained the following;

php:
<?php
    
function datediff($start_date,$end_date,$unit="y")
        {
            
$unit strtoupper($unit);
            
$start=strtotime($start_date);
            if (
$start === -1) {
                print(
"invalid start date");
            }
            
            
$end=strtotime($end_date);            
            if (
$end === -1) {
                print(
"invalid end date");
            }
            
            if (
$start $end) {
                
$temp $start;
                
$start $end;
                
$end $temp;
            }
            
            
$diff $end-$start;
            
            
$day1 date("j"$start);
            
$mon1 date("n"$start);
            
$year1 date("Y"$start);
            
$day2 date("j"$end);
            
$mon2 date("n"$end);
            
$year2 date("Y"$end);
            
            switch(
$unit) {
                case 
"D":
                    print(
intval($diff/(24*60*60)));
                    break;
                case 
"M":
                    if(
$day1>$day2) {
                        
$mdiff = (($year2-$year1)*12)+($mon2-$mon1-1);
                    } else {
                        
$mdiff = (($year2-$year1)*12)+($mon2-$mon1);
                    }
                    print(
$mdiff);
                    break;
                case 
"Y":
                    if((
$mon1>$mon2) || (($mon1==$mon2) && ($day1>$day2))){
                        
$ydiff $year2-$year1-1;
                    } else {
                        
$ydiff $year2-$year1;
                    }
                    print(
$ydiff);
                    break;
                case 
"YM":
                    if(
$day1>$day2) {
                        if(
$mon1>=$mon2) {
                            
$ymdiff 12+($mon2-$mon1-1);
                        } else {
                            
$ymdiff $mon2-$mon1-1;
                        }
                    } else {
                        if(
$mon1>$mon2) {
                            
$ymdiff 12+($mon2-$mon1);
                        } else {
                            
$ymdiff $mon2-$mon1;
                        }
                    }
                    print(
$ymdiff);
                    break;
                case 
"YD":
                    if((
$mon1>$mon2) || (($mon1==$mon2) &&($day1>$day2))) {
                        
$yddiff intval(($end mktime(000$mon1$day1$year2-1))/(24*60*60));                        
                    } else {
                        
$yddiff intval(($end mktime(000$mon1$day1$year2))/(24*60*60));
                    }
                    print(
$yddiff);
                    break;
                case 
"MD":
                    if(
$day1>$day2) {
                        
$mddiff intval(($end mktime(000$mon2-1$day1$year2))/(24*60*60));                        
                    } else {
                        
$mddiff intval(($end mktime(000$mon2$day1$year2))/(24*60*60));
                    }
                    print(
$mddiff);
                    break;
                default:
                 print(
"{Datedif Error: Unrecognized \$unit parameter. Valid values are 'Y', 'M', 'D', 'YM'. Default is 'D'.}");
                
            }

        }

?>



Then inlcuded it;

php:
<?php 
include('dateDiff.php'); 
?>



Then added the code to my page;

php:
<?php 
          $date_1 
$row_member_profile['dog_dob'];
          
$date_2 $row_member_profile['years'];
          
datediff ("$date_1","$date_2","y"); 
?>

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...