Advertisement
killerbng

Get Age

Mar 16th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. /*
  2.  * Here are several ways to do this, depending on how much accuracy/info you want.
  3.  */
  4.  
  5. //Ex: 5.3482484954333
  6. $age = floor(time() - strtotime('2009-11-09')) / 31556926;
  7.  
  8. //Ex: You are 05 Years, 04 Months, 7 Days Old
  9. $interval = date_diff(date_create(), date_create('2009-11-09'));
  10. echo $interval->format("You are %Y Years, %M Months, %d Days Old");
  11.  
  12. //Ex: You are %Y Years, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old       
  13. $interval = date_diff(date_create(), date_create('2009-11-09 00:00:00'));
  14. echo $interval->format("You are %Y Years, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");  
  15.  
  16.  
  17. //Ex: findage("21-04-1969"); == Age is $years years $months months.
  18. //date_default_timezone_set('Europe/Moscow');
  19. ini_set("date.timezone", "Europe/Moscow");//I prefer this over above
  20. Exfunction findage($dob)
  21. {
  22.     $localtime = getdate();
  23.     $today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];
  24.     $dob_a = explode("-", $dob);
  25.     $today_a = explode("-", $today);
  26.     $dob_d = $dob_a[0];$dob_m = $dob_a[1];$dob_y = $dob_a[2];
  27.     $today_d = $today_a[0];$today_m = $today_a[1];$today_y = $today_a[2];
  28.     $years = $today_y - $dob_y;
  29.     $months = $today_m - $dob_m;
  30.     if ($today_m.$today_d < $dob_m.$dob_d)
  31.     {
  32.         $years--;
  33.         $months = 12 + $today_m - $dob_m;
  34.     }
  35.  
  36.     if ($today_d < $dob_d)
  37.     {
  38.         $months--;
  39.     }
  40.  
  41.     $firstMonths=array(1,3,5,7,8,10,12);
  42.     $secondMonths=array(4,6,9,11);
  43.     $thirdMonths=array(2);
  44.  
  45.     if($today_m - $dob_m == 1)
  46.     {
  47.         if(in_array($dob_m, $firstMonths))
  48.         {
  49.             array_push($firstMonths, 0);
  50.         }
  51.         elseif(in_array($dob_m, $secondMonths))
  52.         {
  53.             array_push($secondMonths, 0);
  54.         }elseif(in_array($dob_m, $thirdMonths))
  55.         {
  56.             array_push($thirdMonths, 0);
  57.         }
  58.     }
  59.     echo "<br><br> Age is $years years $months months.";
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement