Date and Birthday php
Reading the positional numbers use php substr() function. For an example take the date 2012-10- 17 add the date into substr() function. as bellow.
<?php $num="2012-06-17"; echo "year:-". substr($num, 0, 4)."</br>"; echo "Month:-". substr($num, 5,2)."</br>"; echo "Date:-". substr($num, 8, 2)."</br>"; ?> |
Output is year:-2012 Month:-06 Date:-17 |
Get Birthday in years
You can get age in years by using codes bellow.
<?php $Birthdate="1965-06-12"; function GetAge($Birthdate) { list($BirthYear,$BirthMonth,$BirthDay) = explode("-", $Birthdate); $YearDiff = date("Y") - $BirthYear; $MonthDiff = date("m") - $BirthMonth; $DayDiff = date("d") - $BirthDay; if ($DayDiff < 0 || $MonthDiff < 0) $YearDiff--; return $YearDiff; } echo GetAge($Birthdate)." "."Years Old"; ?> |
Output is 47 Years Old |
Get Birthday in days,month and years
Execute the following codes to get birthday in details with your input of birthday.
| ||||||
<form name="f1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table bgcolor=#d38a2a align="center"><tr bgcolor=#ffffff><td>Bith Day</td><td><input type="text" name="bday"></td></tr> <tr bgcolor=#ffffff><td>Click To Find Age</td><td align="right"><input type="submit" value="Find"></td></tr> </form> <?php $bday=$_POST['bday']; function get_Age_difference($start_date,$end_date){ list($start_year,$start_month,$start_date) = split('-', $start_date); list($current_year,$current_month,$current_date) = split('-', $end_date); $result = ''; for($x=1 ; $x<=12 ; $x++){ $dim[$x] = date('t',mktime(0,0,0,$x,1,date('Y'))); } $m = $current_month - $start_month; $d = $current_date - $start_date; $y = $current_year - $start_year; if($d < 0) { $today_day = $current_date + $dim[$current_month]; $today_month = $current_month - 1; $d = $today_day - $start_date; $m = $today_month - $start_month; if(($today_month - $start_month) < 0) { $today_month += 12; $today_year = $current_year - 1; $m = $today_month - $start_month; $y = $today_year - $start_year; } } if($m < 0) { $today_month = $current_month + 12; $today_year = $current_year - 1; $m = $today_month - $start_month; $y = $today_year - $start_year; } if($y < 0) { die("Start Date Entered is a Future date than End Date."); } else { switch($y) { case 0 : $result .= ''; break; case 1 : $result .= $y.($m == 0 && $d == 0 ? ' year old' : ' year'); break; default : $result .= $y.($m == 0 && $d == 0 ? ' years old' : ' years'); } switch($m) { case 0: $result .= ''; break; case 1: $result .= ($y == 0 && $d == 0 ? $m.' month old' : ($y == 0 && $d != 0 ? $m.' month' : ($y != 0 && $d == 0 ? ' and '.$m.' month old' : ', '.$m.' month'))); break; default: $result .= ($y == 0 && $d == 0 ? $m.' months old' : ($y == 0 && $d != 0 ? $m.' months' : ($y != 0 && $d == 0 ? ' and '.$m.' months old' : ', '.$m.' months'))); break; } switch($d) { case 0: $result .= ($m == 0 && $y == 0 ? 'Today' : ''); break; case 1: $result .= ($m == 0 && $y == 0 ? $d.' day old' : ($y != 0 || $m != 0 ? ' and '.$d.' day old' : '')); break; default: $result .= ($m == 0 && $y == 0 ? $d.' days old' : ($y != 0 || $m != 0 ? ' and '.$d.' days old' : '')); } } return $result; } $date_difference= get_Age_difference($bday,date("Y-m-d")); echo "<tr bgcolor=#ffffff><td>Age</td><td>".$date_difference."</td></tr></table>"; ?> |
No comments:
Post a Comment