View: 10100|Reply: 19
|
Help : Macam mana nak kira Umur dari DOB using PHP
[Copy link]
|
|
Salam guys,
sory baru nak menapak dalam php.
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.
anyone, help me. thanks |
|
|
|
|
|
|
|
- <?
- 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;
- }
- $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';
- ?>
Copy the Code |
|
|
|
|
|
|
|
Post Last Edit by ana_una at 2-11-2009 16:28
$born_date = mktime(6,30,1,4,15,1977); <----- befday tuan manager ke ni?Glemer tauuu...
ok... ni modified code yg bole tambah utk baca variable utk tarikh lahir..
sori erkk tuan manager.. haijek skrip skit... kalo salah tolong betoikan...
- <?
- 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); //pecah2kan 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';
- ?>
Copy the Code |
|
|
|
|
|
|
|
apasal dah tukar kaler tamo kuar kaler??? |
|
|
|
|
|
|
|
peh.. bijok akak n... hehe tu bday sapa tah.... |
|
|
|
|
|
|
|
terima kasih banyak2 atas pertolongan.
dah berjaya.
|
|
|
|
|
|
|
|
5# BorderManager
toche..toche...
tu pasal lama kene benam..
peram lama2... |
|
|
|
|
|
|
|
mintak tulun sekali lagi boleh?
date saya format dia d-m-y, so bila sy convert ker y-m-d dia jadi tapi dia x leh calculate..kenapa yer
ni koding yg sy pakai tok convert
$born_date="02/03/1979"; // call variable tarikh lahir tukar ke ---> $born_date=$row['born_date'];
echo 'Tarikh lahir =' .$born_date;
echo "<br>";
$born_date=explode ("/",$born_date);
krsort($born_date); print "<p>".implode("/",$born_date)."</p>"; tu
$born_date = mktime(6,30,1,$born_date[1],$born_date[2],$born_date[0]);
echo $born_date; |
|
|
|
|
|
|
|
ko terbalik tu...
$born_date = mktime(6,30,1,$born_date[1],$born_date[0],$born_date[2]);
mktime ($hour, $minute, $second, $month, $day, $year); |
|
|
|
|
|
|
|
thank u bro |
|
|
|
|
|
|
|
wah, ada pakar PHP la..
nak copy nih he4.. |
|
|
|
|
|
|
|
copy je.. FOC
BorderManager Post at 8-11-2009 23:50
kalo camtu nk claim pd tuan manager le... |
|
|
|
|
|
|
|
Post Last Edit by isms at 22-3-2011 08:04
edited.... |
|
|
|
|
|
|
|
Reply 13# BorderManager
macam mana pulak kalau kita nak kira date_diff antara dua tarikh.
cth kalau saya ada date1=13/4/1999 dan date2=01/03/2011,
sy cuba modified coding BM bagi tp x jadilah, output asik 41y, 2m, 0 d
sy wt cam ne
------------------------------------------------------------
$date_diff;
$born_date=$date2;
$born_date=explode ("/",$born_date);
krsort($born_date);
implode("/",$born_date);
$born_date = mktime(6,30,1,$born_date[1],$born_date[0],$born_date[2]);
$date_diff_array = date_diff($date1, $born_date);
$beza = $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d';
--------------------------------
boleh tolong bgtau tak tang mana yg salah... |
|
|
|
|
|
|
|
- <?php
- 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;
- }
- $date1 = '13/4/1999';
- $date1 = explode ("/",$date1);
- $date1 = mktime(0, 0, 0, $date1[1], $date1[0], $date1[2]);
- $date2 = '01/03/2011';
- $date2=explode ("/",$date2);
- $date2 = mktime(0, 0, 0, $date2[1], $date2[0], $date2[2]);
- $date_diff_array = date_diff($date1, $date2);
- echo 'Date diff = '. $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d';
- ?>
Copy the Code |
|
|
|
|
|
|
|
Reply 17# BorderManager
terima kasih banyak2 bro. |
|
|
|
|
|
|
|
Bro BorderManager,
sy ada koding spt kat bawah ne.
sy dh buat if else statement utk beza kan kaler font untuk value yg berbeza2
cthnya, kalau value tuh lebih >=0 dan <=14, font tukar kaler hijo
kalau selain tuh kaler merah.
sy rs if else stament tuh dh betui dh
cumanya, dia mcm x boleh bc value tuh
menyebablan semua kaler hijo shj wpun ada yg lebih dr 14 hari.
lepas tuh mcm mana yer sy nk convert tahun dh bulan ke bilangan hari shj
cth 1y 2m 10 day(ne cth output), sbb ne kot dia x recognize value, menyebabkan semua jadi kaler hijo.
cth tuh sy nak convert ke integer shj, tok menjadikan ker 435 hari. contohnya lah.
$accComplDt = trim($row['ACC_COMPL_DT']);
$date1 = $accComplDt;//'01/09/2015';
$date1 = explode ("/",$date1);
$date1 = mktime(0, 0, 0, $date1[1], $date1[0], $date1[2]);
$accDate = trim($row['ACC_DATE']);
$date2 = $accDate;//'02/10/2015';
$date2=explode ("/",$date2);
$date2 = mktime(0, 0, 0, $date2[1], $date2[0], $date2[2]);
$date_diff_array = date_diff($date1, $date2);
$Tempoh = $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d';
//$Tempoh = $date_diff_array['day'] .' d';
if (($Tempoh >= 0) ||(Tempoh <=14))
{
echo "<font color=\"#006600\">" .$Tempoh;
} else {
echo "<font color=\"#FF0000\">" .$Tempoh;
}
|
|
|
|
|
|
|
|
query dlm SQL terus je
"SELECT IF(DATEDIFF(ACC_COMPL_DT, ACC_COMPL_DT) AS day FROM table";
print '<span style="color: '. ($row['day'] >= 0 || $row['day'] <= 14 ? '#006600' : '#FF0000') .'">'. $row['day'] .'</span>';
|
|
|
|
|
|
|
| |
|