CariDotMy

 Forgot password?
 Register

ADVERTISEMENT

View: 10100|Reply: 19

Help : Macam mana nak kira Umur dari DOB using PHP

[Copy link]
Post time 2-11-2009 12:02 PM | Show all posts |Read mode
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
Reply

Use magic Report


ADVERTISEMENT


Post time 2-11-2009 02:12 PM | Show all posts
  1. <?
  2. function date_diff($d1, $d2)
  3. {
  4.   if ($d1 < $d2){
  5.     $temp = $d2;
  6.     $d2 = $d1;
  7.     $d1 = $temp;
  8.   }else{
  9.     $temp = $d1;
  10.   }
  11.   $d1 = date_parse(date("Y-m-d H:i:s",$d1));
  12.   $d2 = date_parse(date("Y-m-d H:i:s",$d2));
  13.   //seconds
  14.   if ($d1['second'] >= $d2['second']){
  15.     $diff['second'] = $d1['second'] - $d2['second'];
  16.   }
  17.   else {
  18.     $d1['minute']--;
  19.     $diff['second'] = 60-$d2['second']+$d1['second'];
  20.   }
  21.   //minutes
  22.   if ($d1['minute'] >= $d2['minute']){
  23.     $diff['minute'] = $d1['minute'] - $d2['minute'];
  24.   }
  25.   else {
  26.     $d1['hour']--;
  27.     $diff['minute'] = 60-$d2['minute']+$d1['minute'];
  28.   }
  29.   //hours
  30.   if ($d1['hour'] >= $d2['hour']){
  31.     $diff['hour'] = $d1['hour'] - $d2['hour'];
  32.   }
  33.   else {
  34.     $d1['day']--;
  35.     $diff['hour'] = 24-$d2['hour']+$d1['hour'];
  36.   }
  37.   //days
  38.   if ($d1['day'] >= $d2['day']){
  39.     $diff['day'] = $d1['day'] - $d2['day'];
  40.   }
  41.   else {
  42.     $d1['month']--;
  43.     $diff['day'] = date("t",$temp)-$d2['day']+$d1['day'];
  44.   }
  45.   //months
  46.   if ($d1['month'] >= $d2['month']){
  47.     $diff['month'] = $d1['month'] - $d2['month'];
  48.   }
  49.   else {
  50.     $d1['year']--;
  51.     $diff['month'] = 12-$d2['month']+$d1['month'];
  52.   }
  53.   //years
  54.   $diff['year'] = $d1['year'] - $d2['year'];
  55.   return $diff;   
  56. }

  57. $born_date = mktime(6,30,1,4,15,1977);
  58. $date_diff_array = date_diff($born_date, time());
  59. echo 'Age = '. $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d';
  60. ?>
Copy the Code
Reply

Use magic Report

Post time 2-11-2009 04:23 PM | Show all posts
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...

  1. <?
  2. function date_diff($d1, $d2)
  3. {
  4.   if ($d1 < $d2){
  5.     $temp = $d2;
  6.     $d2 = $d1;
  7.     $d1 = $temp;
  8.   }else{
  9.     $temp = $d1;
  10.   }
  11.   $d1 = date_parse(date("Y-m-d H:i:s",$d1));
  12.   $d2 = date_parse(date("Y-m-d H:i:s",$d2));
  13.   //seconds
  14.   if ($d1['second'] >= $d2['second']){
  15.     $diff['second'] = $d1['second'] - $d2['second'];
  16.   }
  17.   else {
  18.     $d1['minute']--;
  19.     $diff['second'] = 60-$d2['second']+$d1['second'];
  20.   }
  21.   //minutes
  22.   if ($d1['minute'] >= $d2['minute']){
  23.     $diff['minute'] = $d1['minute'] - $d2['minute'];
  24.   }
  25.   else {
  26.     $d1['hour']--;
  27.     $diff['minute'] = 60-$d2['minute']+$d1['minute'];
  28.   }
  29.   //hours
  30.   if ($d1['hour'] >= $d2['hour']){
  31.     $diff['hour'] = $d1['hour'] - $d2['hour'];
  32.   }
  33.   else {
  34.     $d1['day']--;
  35.     $diff['hour'] = 24-$d2['hour']+$d1['hour'];
  36.   }
  37.   //days
  38.   if ($d1['day'] >= $d2['day']){
  39.     $diff['day'] = $d1['day'] - $d2['day'];
  40.   }
  41.   else {
  42.     $d1['month']--;
  43.     $diff['day'] = date("t",$temp)-$d2['day']+$d1['day'];
  44.   }
  45.   //months
  46.   if ($d1['month'] >= $d2['month']){
  47.     $diff['month'] = $d1['month'] - $d2['month'];
  48.   }
  49.   else {
  50.     $d1['year']--;
  51.     $diff['month'] = 12-$d2['month']+$d1['month'];
  52.   }
  53.   //years
  54.   $diff['year'] = $d1['year'] - $d2['year'];
  55.   return $diff;   
  56. }
  57. ////////////////////// edit disini //////////////////////////////////////////
  58. $born_date="1977-04-15"; // call variable tarikh lahir tukar ke ---> $born_date=$row['born_date'];
  59. $born_date=explode ("-",$born_date); //pecah2kan tarikh lahir tu
  60. $born_date = mktime(6,30,1,$born_date[1],$born_date[2],$born_date[0]);
  61. ////////////////////// tamat edit disini ///////////////////////////////////

  62. //$born_date = mktime(6,30,1,4,15,1977);
  63. $date_diff_array = date_diff($born_date, time());
  64. echo 'Age = '. $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d';
  65. ?>
Copy the Code
Reply

Use magic Report

Post time 2-11-2009 04:25 PM | Show all posts
apasal dah tukar kaler tamo kuar kaler???
Reply

Use magic Report

Post time 2-11-2009 04:32 PM | Show all posts
peh.. bijok akak n... hehe tu bday sapa tah....
Reply

Use magic Report

 Author| Post time 2-11-2009 04:38 PM | Show all posts
terima kasih banyak2 atas pertolongan.
dah berjaya.
Reply

Use magic Report

Follow Us
Post time 2-11-2009 04:51 PM | Show all posts
5# BorderManager

toche..toche...
tu pasal lama kene benam..
peram lama2...
Reply

Use magic Report

Post time 2-11-2009 04:53 PM | Show all posts
terima kasih banyak2 atas pertolongan.
dah berjaya.

alisetan Post at 2-11-2009 16:38


sama-sama banyak2...
Reply

Use magic Report


ADVERTISEMENT


 Author| Post time 3-11-2009 10:43 AM | Show all posts
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;
Reply

Use magic Report

Post time 3-11-2009 12:11 PM | Show all posts
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);
Reply

Use magic Report

 Author| Post time 3-11-2009 01:12 PM | Show all posts
thank u bro
Reply

Use magic Report

Post time 8-11-2009 12:56 AM | Show all posts
wah, ada pakar PHP la..
nak copy nih he4..
Reply

Use magic Report

Post time 8-11-2009 11:50 PM | Show all posts
copy je.. FOC
Reply

Use magic Report

Post time 9-11-2009 10:10 AM | Show all posts
copy je.. FOC
BorderManager Post at 8-11-2009 23:50


kalo camtu nk claim pd tuan manager le...
Reply

Use magic Report

Post time 21-3-2011 05:52 PM | Show all posts
Post Last Edit by isms at 22-3-2011 08:04

edited....
Reply

Use magic Report

Post time 22-3-2011 08:04 AM | Show all posts
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...
Reply

Use magic Report


ADVERTISEMENT


Post time 22-3-2011 10:32 AM | Show all posts
  1. <?php
  2. function date_diff($d1, $d2)
  3. {
  4.   if ($d1 < $d2){
  5.     $temp = $d2;
  6.     $d2 = $d1;
  7.     $d1 = $temp;
  8.   }else{
  9.     $temp = $d1;
  10.   }
  11.   $d1 = date_parse(date("Y-m-d H:i:s",$d1));
  12.   $d2 = date_parse(date("Y-m-d H:i:s",$d2));
  13.   //seconds
  14.   if ($d1['second'] >= $d2['second']){
  15.     $diff['second'] = $d1['second'] - $d2['second'];
  16.   }
  17.   else {
  18.     $d1['minute']--;
  19.     $diff['second'] = 60-$d2['second']+$d1['second'];
  20.   }
  21.   //minutes
  22.   if ($d1['minute'] >= $d2['minute']){
  23.     $diff['minute'] = $d1['minute'] - $d2['minute'];
  24.   }
  25.   else {
  26.     $d1['hour']--;
  27.     $diff['minute'] = 60-$d2['minute']+$d1['minute'];
  28.   }
  29.   //hours
  30.   if ($d1['hour'] >= $d2['hour']){
  31.     $diff['hour'] = $d1['hour'] - $d2['hour'];
  32.   }
  33.   else {
  34.     $d1['day']--;
  35.     $diff['hour'] = 24-$d2['hour']+$d1['hour'];
  36.   }
  37.   //days
  38.   if ($d1['day'] >= $d2['day']){
  39.     $diff['day'] = $d1['day'] - $d2['day'];
  40.   }
  41.   else {
  42.     $d1['month']--;
  43.     $diff['day'] = date("t",$temp)-$d2['day']+$d1['day'];
  44.   }
  45.   //months
  46.   if ($d1['month'] >= $d2['month']){
  47.     $diff['month'] = $d1['month'] - $d2['month'];
  48.   }
  49.   else {
  50.     $d1['year']--;
  51.     $diff['month'] = 12-$d2['month']+$d1['month'];
  52.   }
  53.   //years
  54.   $diff['year'] = $d1['year'] - $d2['year'];
  55.   return $diff;   
  56. }

  57. $date1 = '13/4/1999';
  58. $date1 = explode ("/",$date1);
  59. $date1 = mktime(0, 0, 0, $date1[1], $date1[0], $date1[2]);

  60. $date2 = '01/03/2011';
  61. $date2=explode ("/",$date2);
  62. $date2 = mktime(0, 0, 0, $date2[1], $date2[0], $date2[2]);

  63. $date_diff_array = date_diff($date1, $date2);
  64. echo 'Date diff = '. $date_diff_array['year'] .' y '. $date_diff_array['month'].' m '. $date_diff_array['day'] .' d';

  65. ?>
Copy the Code
Reply

Use magic Report

Post time 22-3-2011 11:41 AM | Show all posts
Reply 17# BorderManager

terima kasih banyak2 bro.
Reply

Use magic Report

Post time 7-10-2015 03:36 PM | Show all posts

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;
                        
                             }


Reply

Use magic Report

Post time 13-10-2015 01:56 PM | Show all posts
isms replied at 7-10-2015 03:36 PM
Bro BorderManager,

sy ada koding spt kat bawah ne.

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>';
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT


Forum Hot Topic

 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

27-12-2024 04:21 PM GMT+8 , Processed in 0.079612 second(s), 33 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list