Assalam-o-Alaikum!
Today we are going to learn that “How to Calculate Date of Birth in PHP“. I am giving simple code you should change values in code and see the result. Code is here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php error_reporting(0); function leapYearFeb($year, $mon) //Leap year checking { $flag = 0; if ($year % 100 == 0) { if ($year % 400 == 0) { if ($mon == 2) { $flag = 1; } } } else if ($year % 4 == 0) { if ($mon == 2) { $flag = 1; } } return ($flag); } $daysInMon = array(31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31); $day=15; $month=11; $year=1986; echo "\nDate of Birth: ".$day. "/" .$month. "/" .$year; echo "\nCurrent Date: ".date("d"). "/" .date("m"). "/" .date("Y"); if (leapYearFeb($year, $month)) //Checking the given year Leap or not { $days = $days + 1; } $days1 = $days + date("d"); $month1 = (12 - $month) + date("m"); $year1 = date("Y")- $year-1; if (leapYearFeb(date("Y"), (date("m") + 1))) { if ($days >= ($daysInMon[date("m")] + 1)) { $days = $days - ($daysInMon[date("m")] + 1); $month = $month + 1; } } else if ($days >= $daysInMon[date("m")]) { $days = $days - ($daysInMon[date("m")]); $month = $month + 1; } if ($month >= 12) { $year = $year + 1; $month = $month - 12; } echo "\nAge :" .$year1."years". $month1. "months" .$days1."days"; ?> |
Now save this file and run it.