Ali tgh develope satu sistem yang memerlukan pengiraan umur. Igatkan masalah dah solve bila ali jupe satu kding tuh, mmg dia leh kira umur. tp kira umur dr tahun lahir jer. So sekarang nih bos ali nak suruh ali cari plak pengiraan yang exactly boleh kira hari dan bulan lahir. Cth : katalah ali lahir 1/12/1980..
kalau kira by tahun umur ALi hari ni (2/11/2009)ialah 29 tahun,
tapi bos ali x nak mcm tuh dia nak umur tuh tepat cam umur Alli hari ini ialah 28 tahun 11 bulan 1 hari.
Cara Penyelesaian :
<? function date_diff($d1, $d2) { if ($d1 < $d2){ $temp = $d2; $d2 = $d1; $d1 = $temp; }else{ $temp = $d1; } $d1 = date_parse(date("Y-m-d H:i:s",$d1)); $d2 = date_parse(date("Y-m-d H:i:s",$d2)); //seconds if ($d1['second'] >= $d2['second']){ $diff['second'] = $d1['second'] - $d2['second']; } else { $d1['minute']--; $diff['second'] = 60-$d2['second']+$d1['second']; } //minutes if ($d1['minute'] >= $d2['minute']){ $diff['minute'] = $d1['minute'] - $d2['minute']; } else { $d1['hour']--; $diff['minute'] = 60-$d2['minute']+$d1['minute']; } //hours if ($d1['hour'] >= $d2['hour']){ $diff['hour'] = $d1['hour'] - $d2['hour']; } else { $d1['day']--; $diff['hour'] = 24-$d2['hour']+$d1['hour']; } //days if ($d1['day'] >= $d2['day']){ $diff['day'] = $d1['day'] - $d2['day']; } else { $d1['month']--; $diff['day'] = date("t",$temp)-$d2['day']+$d1['day']; } //months if ($d1['month'] >= $d2['month']){ $diff['month'] = $d1['month'] - $d2['month']; } else { $d1['year']--; $diff['month'] = 12-$d2['month']+$d1['month']; } //years $diff['year'] = $d1['year'] - $d2['year']; return $diff; } //// edit disini /////// $born_date="1977-04-15"; // call variable tarikh lahir tukar ke ---> $born_date=$row['born_date']; $born_date=explode ("-",$born_date); //pecah -pecahkan kan tarikh lahir tu $born_date = mktime(6,30,1,$born_date[1],$born_date[2],$born_date[0]); /// tamat edit disini /////// //$born_date = mktime(6,30,1,4,15,1977); $date_diff_array = date_diff($born_date, time()); echo 'Age = '. $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d'; ?>
Tukar Format date dari d-m-y kepada y-m-d
<?php $born_date = mktime(6,30,1,$born_date[1],$born_date[0],$born_date[2]); mktime ($hour, $minute, $second, $month, $day, $year); ?>
0 comments:
Post a Comment