Skip to content

Instantly share code, notes, and snippets.

@sineld
Created October 5, 2012 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sineld/3838327 to your computer and use it in GitHub Desktop.
Save sineld/3838327 to your computer and use it in GitHub Desktop.
Php: tarihFarki
<?php
function tarihFarki($startDate, $endDate)
{
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
if ($startDate === false || $startDate < 0 || $endDate === false || $endDate < 0 || $startDate > $endDate)
return false;
$years = date('Y', $endDate) - date('Y', $startDate);
$endMonth = date('m', $endDate);
$startMonth = date('m', $startDate);
// Ayları hesapla
$months = $endMonth - $startMonth;
if ($months <= 0) {
$months += 12;
$years--;
}
if ($years < 0)
return false;
// Günleri hesapla
$offsets = array();
if ($years > 0)
$offsets[] = $years . (($years == 1) ? ' year' : ' years');
if ($months > 0)
$offsets[] = $months . (($months == 1) ? ' month' : ' months');
$offsets = count($offsets) > 0 ? '+' . implode(' ', $offsets) : 'now';
$days = $endDate - strtotime($offsets, $startDate);
$days = date('z', $days);
return array($years, $months, $days);
}
$basla = date('2012-06-01');
$bitis = date('2013-12-14');
$fark = tarihFarki($basla, $bitis);
echo $basla. ' ve '.$bitis. ' tarihleri arasında '.$fark[0]. ' yıl, '. $fark[1]. ' ay, '. $fark[2].' gün fark var.';
// 2012-06-01 ve 2013-12-14 tarihleri arasında 1 yıl, 6 ay, 13 gün fark var.
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment