Fungsi PHP untuk menghitung selisih atau jarak 2 tanggal

Menghitung selisih atau jarak antara 2 tanggal bukanlah hal sulit menggunakan script PHP. tapi itu setelah saya menemukan script ini ... ini sudah lama saat saya sedang mengerjakan suatu aplikasi dan ternyata terdapat beberapa bagian yg mengharuskan menghitung jarak antara 2 tanggal menggunakan script php.

bukannya plagiat, tapi saya hanya ingin berbagi semoga ada yg merasa membutuhkan dan terbantu.
dan bagi yang tau sumbernya juga boleh ditulis.. karena fungsi ini bukan saya yang membuat saya hanya tinggal memanfaatkannya, :-)

yah langsung aja, pasti kalo temen - temen lagi baca udah ga sabar :


<?php
  // Set timezone
  date_default_timezone_set("UTC");

  // Time format is UNIX timestamp or
  // PHP strtotime compatible strings
  function dateDiff($time1, $time2, $precision = 6) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
      $time2 = strtotime($time2);
    }

    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }

    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();

    // Loop thru all intervals
    foreach ($intervals as $interval) {
      // Set default diff to 0
      $diffs[$interval] = 0;
      // Create temp time from time1 and interval
      $ttime = strtotime("+1 " . $interval, $time1);
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
      }
    }

    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
      // Break if we have needed precission
      if ($count >= $precision) {
break;
      }
      // Add value and interval 
      // if value is bigger than 0
      if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
 $interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
      }
    }

    // Return string with times
    return implode(", ", $times);
  }
?>

nah tadi itu script yang berisikan fungsi datediff();
akan lebih baik kamu buat dalam 1 file tersendiri misal dinamakan datediff.php
dan nantinya ketika kamu memperlukan fungsi ini cukup memanggilnya menggunakan include ataupun require di php. terserahlah. :-) nah selanjutnya saya berikan sedikit contoh pemakaian atau cara memakai fungsi datediff ini :


fungsi di php :

<?php
include('datediff.php');
echo dateDiff("2010-01-26", "2004-01-26") . "\n"; 
echo dateDiff("2006-04-12 12:30:00", "1987-04-12 12:30:01") . "\n"; 
echo dateDiff("now", "now +2 months") . "\n"; 
echo dateDiff("now", "now -6 year -2 months -10 days") . "\n";
echo dateDiff("2009-01-26", "2004-01-26 15:38:11") . "\n";
?>

Hasil di broser :
6 years
18 years, 11 months, 30 days, 23 hours, 59 minutes, 59 seconds
2 months
6 years, 2 months, 10 days
4 years, 11 months, 30 days, 8 hours, 21 minutes, 49 seconds

================================================

Penulisan fungsi :
<?php
include('datediff.php');
echo dateDiff(time(), time()-1000000, 1) . "\n";
echo dateDiff(time(), time()-1000000, 3) . "\n"; 
echo dateDiff(time(), time()-1000000, 6) . "\n";
?>
hasilnya :
11 days
11 days, 13 hours, 46 minutes
11 days, 13 hours, 46 minutes, 40 seconds

================================================

script php :
<?php
include('datediff.php');
$time1 = time(); 
$time2 = $time1-10000000;
echo $diff = dateDiff($time1, $time2) . "\n"; 
echo $time1 . "\n";
echo strtotime(" +".$diff, $time2) . "\n";
?>
hasilnya :
3 months, 23 days, 17 hours, 46 minutes, 40 seconds
1264514564
1264514564

sekian untuk fungsi kali ini.. I LOVE PHP, dateDiff digunakan untuk mendapatkan nilai jarak/selisih dari 2 tanggal.
semoga bermanfaat :-)

Comments

Post a Comment

Popular posts from this blog

Belajar ExtJS Dasar

Session pada Code Igniter