~:Catatan Belajar MazHel:~

Countdown your birthday in PHP

Posted in PHP by m42h31 on 26 April 2010

Pernahkah terfikir untuk menghitung mundur berapa usia anda saat ini sampai hari ini, usia saya sampai hari ini adalah 23 tahun 6 bulan 7 hari. iseng bikin program pendek untuk melakukan itu (padahal karena client minta begini..hiks..ngerjain banget). kebanyakan di internet beredar script lain namun setelah saya lakukan beberapa pengujian perhitungannya tidak tepat. karena programmer lupa (atau sengaja melupakan.. hehe) jumlah hari dalam 1 bulan tidak bisa dipukul rata & yang terpenting ada tahun kabisat yang jumlah harinya tentu tidak sama dengan tahun biasa.

Berikut ini adalah program cupu yang saya mahsud :

function Formatdate($rawdate,$format){
$dateTime = new DateTime($rawdate);
$formatted_date=date_format ( $dateTime, $format );
return $formatted_date;
}

function hitungTahunLahir($birthday, $today) {
if ($this->Formatdate($birthday,”Y”) <= $this->Formatdate($today,”Y”) &&

$this->Formatdate($birthday,”m”) <= $this->Formatdate($today,”m”) &&

$this->Formatdate($birthday,”d”) <= $this->Formatdate($today,”d”))
return ($this->Formatdate($today,”Y”) – $this->Formatdate($birthday,”Y”));
else {
if ($this->Formatdate($birthday,”m”) > $this->Formatdate($today,”m”) &&

$this->Formatdate($birthday,”d”) > $this->Formatdate($today,”d”))
return ($this->Formatdate($today,”Y”) – $this->Formatdate($birthday,”Y”) – 1);
else {
if ($this->Formatdate($birthday,”m”) > $this->Formatdate($today,”m”))
return ($this->Formatdate($today,”Y”) – $this->Formatdate($birthday,”Y”) – 1);
else {
if ($this->Formatdate($birthday,”m”) == $this->Formatdate($today,”m”) &&

$this->Formatdate($birthday,”d”) > $this->Formatdate($today,”d”))
return ($this->Formatdate($today,”Y”) – $this->Formatdate($birthday,”Y”) – 1);
else
return ($this->Formatdate($today,”Y”) – $this->Formatdate($birthday,”Y”));
}
}
}
}
function hitungBulanLahir($birthday, $today) {
if ($this->Formatdate($birthday,”m”) <= $this->Formatdate($today,”m”) &&

$this->Formatdate($birthday,”d”) <= $this->Formatdate($today,”d”)) return $this->Formatdate($today,”m”) – $this->Formatdate($birthday,”m”);
else {
if ($this->Formatdate($birthday,”m”) <= $this->Formatdate($today,”m”)) {
if ($this->Formatdate($birthday,”d”) > $this->Formatdate($today,”d”)) {
if ($this->Formatdate($birthday,”Y”) < $this->Formatdate($today,”Y”)) {
if ($this->Formatdate($birthday,”m”) == $this->Formatdate($today,”m”)) {
return 12 – ($this->Formatdate($today,”m”) – $this->Formatdate($birthday,”m”) + 1);
}
else {
return $this->Formatdate($today,”m”) – $this->Formatdate($birthday,”m”) – 1;
}
} else {
return $this->Formatdate($today,”m”) – $this->Formatdate($birthday,”m”) – 1;
}
}
}
else {
if ($this->Formatdate($birthday,”d”) <= $this->Formatdate($today,”d”)) return 12 – ($this->Formatdate($birthday,”m”) – $this->Formatdate($today,”m”));
else return 12 – ($this->Formatdate($birthday,”m”) – $this->Formatdate($today,”m”)) – 1;
}
}
}
function hitungHariLahir($birthday, $today) {
if ($this->Formatdate($birthday,”d”) <= $this->Formatdate($today,”d”)) return $this->Formatdate($today,”d”) – $this->Formatdate($birthday,”d”);
else {
switch ($this->Formatdate($birthday,”m”)) {
Case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31 – $this->Formatdate($birthday,”d”) + $this->Formatdate($today,”d”);
break;
Case 2:
if ($this->Formatdate($birthday,”Y”) % 4 == 0) return 29 – $this->Formatdate($birthday,”d”) + $this->Formatdate($today,”d”);
else 28 – $this->Formatdate($birthday,”d”) + $this->Formatdate($today,”d”);
break;
default:
return 30 – $this->Formatdate($birthday,”d”) + $this->Formatdate($today,”d”);
break;
}
}
}

untuk menggunakan program ini cukup panggil aja fungsi-fungsi diatas, contoh =

function hitungUmur(){

$today = “2010-04-26″;

$birthDay=”1987-04-15”;
$tahun = $this->hitungTahunLahir($birthDay,$today);
$bulan = $this->hitungBulanLahir($birthDay,$today);
$’hari’ = $this->hitungHariLahir($birthDay,$today);
echo “Umur Anda :  “.$tahun.” tahun “.$’bulan.” bulan “.$hari.” hari”;
}

semoga bermanfaat.. kritik dan saran is welcome..🙂

happy coding..!!

Tagged with: , ,

4 Tanggapan

Subscribe to comments with RSS.

  1. newbie said, on 7 Mei 2010 at 09:29

    panjang amat, perasaan bisa pakai datediff aja deh😀, selebihnya tinggal dicasting ke integer, dan dicoding dikit, tapi ga perlu sebanyak itu🙂, he..he..he..

    nice post🙂

    • mazhel said, on 7 Mei 2010 at 23:19

      mahsudnya yang begini ??
      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)
      );
      }
      $a = datediff(“1979-02-07”, “2009-08-24”);

      echo ‘umur ‘.$a[years].’ tahun ‘.$a[months].’ bulan’;
      ?>

      pertanyaannya, bisa menangani tahun kabisat gak ?? hehehe.. saya sudah test coding ini kemudian dibandingkan dengan perhitungan manual.. hasilnya jauh..🙂

      sampai saat ini coding saya diatas masih akurat, tp kalau ada coding yang lebih simple boleh juga tuh.. hehehe..
      thanks kunjungannya yaaa…

  2. y said, on 8 Mei 2010 at 19:51

    bukannya
    $today = “2010-04-26″;
    $birthDay=”1987-04-15″;
    kalau dikurangkan jadi : 23 tahun 0 bulan 12 hari ( karena tgl awal dihitung jadi +1 hari ) ???

    • mazhel said, on 13 Mei 2010 at 01:40

      1987-04-15 bukan tanggal lahir saya pak.. heu heu..
      program diatas jika digunakan untuk menghitung tanggal lahir saya munculnya 23 tahun 6 bulan 7 hari..🙂


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: