Created
October 5, 2012 05:59
-
-
Save sineld/3838327 to your computer and use it in GitHub Desktop.
Php: tarihFarki
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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