sponsor

Showing posts with label calculate. Show all posts
Showing posts with label calculate. Show all posts

Wednesday, December 31, 2008

calculate age with mysql

you can use mysql to determine how many years old of your employee is. This is just compute the difference in the year part of the current date and the birth date, then subtract one if the current date occurs earlier in the calendar year than the birth date. The following query shows, for each employee, name, birth date, the current date, and the age in the years. In this example I use table employee which has fields : employee_id, employee_name, birthdate

SELECT employee_id, employee_name, birthdate, CURDATE(),
(YEAR(CURDATE())-YEAR(birthdate))
- (RIGHT(CURDATE(),5)<RIGHT(birthdate,5))
AS age
FROM employee

Friday, October 26, 2007

Fungsi PHP untuk menghitung umur

Umur seseorang dihitung berdasarkan perbedaan tanggal lahirnya dengan tanggal sekarang. Kali ini saya akan memberikan fungsi PHP untuk menghitung umur.


//sebuah function dengan nama hitUmur dan sebuah parameter
function hitUmur($tgllahir) {
$tgl = explode("-", $tgllahir);
// memecah $tgllahir yang tadinya YYYY-MM-DD menjadi array
// $tgl[0] = tahun (YYYY)
// $tgl[a] = bulan (MM)
// $tgl[2] = hari (DD)

$umur = date("Y") - $tgl[0]; //ini untuk ngitung umurnya

if(($tgl[1] > date("m")) || ($tgl[1] == date("m") && date("d") < $tgl[2])) //ngecek apakah tgl lahir dan bulannya belum lewat?
{

$umur -= 1;
}
return $umur;
}

//cara mengakses fungsi
$tgllahirku = "1980-10-12";
echo hitUmur($tgllahirku);
?>