pusatdata icon

php calcuator age umur

pusatdata | PRO | 01/23/24 09:46:09 AM UTC (Edited) | 0 ⭐ | 595 👁️ | Never ⏰ | []
text |

1.63 KB

|

None

|

0 👍

/

0 👎

INI YANG BAGUS:
 <?php
$dob = new DateTime('1978-09-29');
$today = new DateTime;
$age = $today->diff($dob);
echo $age->format('%y year, %m months dan %d days');
?>
 <?php $dob = new DateTime('1978-09-29');
    $today   = new DateTime('today');
    $year = $dob->diff($today)->y;
    $month = $dob->diff($today)->m;
    $day = $dob->diff($today)->d;
    echo "Age is"." ".$year. " year"." ",$month. " months"." ".$day. " days";
?>
=============================
  1. MODEL PERTAMA
<?php
/**
 * Simple PHP age Calculator
 * 
 * Calculate and returns age based on the date provided by the user.
 * @param   date of birth('Format:yyyy-mm-dd').
 * @return  age based on date of birth
 */
function ageCalculator($dob){
    if(!empty($dob)){
        $birthdate = new DateTime($dob);
        $today   = new DateTime('today');
        $age = $birthdate->diff($today)->y;
        return $age;
    }else{
        return 0;
    }
}
$dob = '1992-03-18';
echo ageCalculator($dob);
?>
 ========================
 2. MODEL KEDUA
<?php
  //date in mm/dd/yyyy format; or it can be in other formats as well
  $birthDate = "12/17/1983";
  //explode the date to get month, day and year
  $birthDate = explode("/", $birthDate);
  //get age from date or birthdate
  $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
  echo "Age is:" . $age;
?> 
 ========================
 3. MODEL KETIGA >> SINGKT
 <?php
    $dob='1981-10-07';
    $diff = (date('Y') - date('Y',strtotime($dob)));
    echo $diff;
?>
 

Comments