Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Here are several ways to do this, depending on how much accuracy/info you want.
- */
- //Ex: 5.3482484954333
- $age = floor(time() - strtotime('2009-11-09')) / 31556926;
- //Ex: You are 05 Years, 04 Months, 7 Days Old
- $interval = date_diff(date_create(), date_create('2009-11-09'));
- echo $interval->format("You are %Y Years, %M Months, %d Days Old");
- //Ex: You are %Y Years, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old
- $interval = date_diff(date_create(), date_create('2009-11-09 00:00:00'));
- echo $interval->format("You are %Y Years, %M Months, %d Days, %H Hours, %i Minutes, %s Seconds Old");
- //Ex: findage("21-04-1969"); == Age is $years years $months months.
- //date_default_timezone_set('Europe/Moscow');
- ini_set("date.timezone", "Europe/Moscow");//I prefer this over above
- Exfunction findage($dob)
- {
- $localtime = getdate();
- $today = $localtime['mday']."-".$localtime['mon']."-".$localtime['year'];
- $dob_a = explode("-", $dob);
- $today_a = explode("-", $today);
- $dob_d = $dob_a[0];$dob_m = $dob_a[1];$dob_y = $dob_a[2];
- $today_d = $today_a[0];$today_m = $today_a[1];$today_y = $today_a[2];
- $years = $today_y - $dob_y;
- $months = $today_m - $dob_m;
- if ($today_m.$today_d < $dob_m.$dob_d)
- {
- $years--;
- $months = 12 + $today_m - $dob_m;
- }
- if ($today_d < $dob_d)
- {
- $months--;
- }
- $firstMonths=array(1,3,5,7,8,10,12);
- $secondMonths=array(4,6,9,11);
- $thirdMonths=array(2);
- if($today_m - $dob_m == 1)
- {
- if(in_array($dob_m, $firstMonths))
- {
- array_push($firstMonths, 0);
- }
- elseif(in_array($dob_m, $secondMonths))
- {
- array_push($secondMonths, 0);
- }elseif(in_array($dob_m, $thirdMonths))
- {
- array_push($thirdMonths, 0);
- }
- }
- echo "<br><br> Age is $years years $months months.";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement