I'm still new with php.
This is my code:
<!DOCTYPE html>
<html>
<head>
<title>
Data buku alamat dengan class
</title>
</head>
<?php
class orang
{
public $nama="";
public $jk="";
public $tptLahir="";
public $tglLahir="";
function umur()
{
list($tgl,$bln,$thn) = explode('-',$tglLahir);
$lahir = mktime(0, 0, 0, (int)$bln, (int)$tgl, $thn);
$t = time();
$umur = ($lahir < 0) ? ( $t - $lahir ) : $t - $lahir;
$tahun = 60 * 60 * 24 * 365;
$tahunlahir = $umur / $tahun;
$umursekarang=floor($tahunlahir);
return $umursekarang;
}
function tampilkan()
{
echo "<hr>Nama : ".$this->nama;
echo "<hr>Jenis Kelamin : ".$this->jk;
echo "<hr>Tempat Lahir : ".$this->tptLahir;
echo "<hr>Tanggal Lahir : ".$this->tglLahir;
echo "<hr>Umur : ".$this->umur();
}
}
?>
<body>
<h1>
Script : Data buku alamat dengan class
</h1>
<?php
$orang1= new orang();
$orang1->nama="Jack";
$orang1->jk="laki-laki";
$orang1->tptLahir="Jakarta";
$orang1->tglLahir="12-09-1988";
$orang1->tampilkan();
?>
</body>
</html>
There's no error. The problem is that, the result of $this->umur() was 15, not 26.
It seem the value in public $tglLahir variable not processed by function umur().
Can anyone tell me where did i did wrong and help how to solve it?