Skip to content

Instantly share code, notes, and snippets.

@Aziz-Rahman
Last active August 29, 2015 14:14
Show Gist options
  • Save Aziz-Rahman/dc82fccff763df89e737 to your computer and use it in GitHub Desktop.
Save Aziz-Rahman/dc82fccff763df89e737 to your computer and use it in GitHub Desktop.
Menghitung selisih usia dengan php pd wordpress custom field tanggal lahir
<?php
if ( ! function_exists( 'datediff' ) ) {
function datediff($tgl1, $tgl2){
$tgl1 = strtotime($tgl1);
$tgl2 = strtotime($tgl2);
$diff_secs = abs($tgl1 - $tgl2);
$base_year = min(date("Y", $tgl1), date("Y", $tgl2));
$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
return array(
"years" => date("Y", $diff) - $base_year,
"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
"months" => date("n", $diff) - 1,
"days_total" => floor($diff_secs / (3600 * 24)),
"days" => date("j", $diff) - 1,
"hours_total" => floor($diff_secs / 3600),
"hours" => date("G", $diff),
"minutes_total" => floor($diff_secs / 60),
"minutes" => (int) date("i", $diff),
"seconds_total" => $diff_secs,
"seconds" => (int) date("s", $diff) );
}
$tgl1 = date_i18n( 'd M Y', strtotime( get_field( 'tanggal_lahir' ) ) );// custom field in wordpress. atau ganti dengan tgl lahir jika d kode php
$tgl2 = date("Y/m/d/ h:m:s");
$a = datediff($tgl1, $tgl2);
echo 'Tanggal Lahir : '.$tgl1; echo '<br>';
echo 'Usia : '.$a['years'].' tahun '.$a['months'].' bulan '.$a['days'].' hari '.$a['hours'].' jam '.$a['minutes'].' menit '.$a['seconds'].' detik';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment