Dynamic Dog Age

Discussion in 'PHP' started by johneva, Sep 15, 2011.

  1. #1
    Hi

    I need a little help with some PHP to work out a dogs age and display it in years and months old so its always upto date.

    
    $molly_dob = "16/5/2010" ;
    $todays_date = date('d/m/y') ;
    
    $molly_age = ???????
    
    PHP:
     
    Last edited: Sep 15, 2011
    johneva, Sep 15, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    you can try this.. didn't have time to make it in accurate years but its a good start

    <?php
    
    $dog_bday = "2011-02-08";
    $today = date("Y-m-d");
    
    $datetime_bday  = strtotime($dog_bday);
    $datetime_today = strtotime($today);
    $diff = $datetime_today - $datetime_bday;
    
    $minutes_diff =  $diff / 60;
    $hours_diff =  $minutes_diff / 60;
    $days_diff =  $hours_diff / 24;
    $years_diff =  $days_diff / 365.242199;
    
    // dog years converstion, re: http://www.dogyears.com/
    if(($years_diff >= .17) && ($years_diff < .5))
    	echo '<= 14 months';
    else if(($years_diff >= .5) && ($years_diff < .67))
    	echo '<= 5 years';
    else if(($years_diff >= .67) && ($years_diff < 1))
    	echo '<= 9 years';
    else if(($years_diff >= 1) && ($years_diff < 2))
    	echo '<= 24 years';
    else if(($years_diff >= 2) && ($years_diff < 3))
    	echo '<= 28 years';
    else if(($years_diff >= 3) && ($years_diff < 4))
    	echo '<= 32 years';
    else if(($years_diff >= 4) && ($years_diff < 5))
    	echo '<= 37 years';
    
    
    ?>
    PHP:
     
    JohnnySchultz, Sep 15, 2011 IP
  3. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #3
    Sorry should have been more clear dont need it in dog years, just in human years. Although I do like the idea of doing both now you have come up with that.

    So I could say Molly is 1yr 5 months old and X old in doggy years.
     
    johneva, Sep 15, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    
    <?php 
    
    date_default_timezone_set('America/Denver'); //  set to your server time if necessary 
    
    $molly_dob = "2007-03-24";
    $todays_date = date('y/d/m') ;
    
    $diff = abs(strtotime($todays_date) - strtotime($molly_dob));
    
    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
    
    $molly_age = "$years Years, $months Months, $days Days"; 
    
    echo $molly_age;
    
    ?>
    
    PHP:
    At time of post: 2 years, 8 months, 1 days

    You might like to add an if statement if its: year or years, month or months, day or days, something to remove the letter s
     
    Last edited: Sep 19, 2011
    MyVodaFone, Sep 19, 2011 IP