Created
February 25, 2015 18:08
-
-
Save finwe/7df5c32ab76c20b9b897 to your computer and use it in GitHub Desktop.
Czech date interval format
This file contains hidden or 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 date_interval(DateTime $begin, DateTime $end = NULL) | |
{ | |
if (!$end) { | |
return $begin->format('j. n. Y'); | |
} | |
$begin = array_reverse(str_split($begin->format('j. n. Y'))); | |
$end = array_reverse(str_split($end->format('j. n. Y'))); | |
$pos = count($begin); | |
foreach ($begin as $key => $val) { | |
if ($val !== $end[$key]) { | |
$pos = $key; | |
break; | |
} | |
} | |
$begin = array_slice($begin, $pos > 0 ? $pos - 1 : 0); | |
$str = implode('', array_reverse($end)); | |
if (count($begin) > 1) { | |
$str = implode('', array_reverse($begin)) . ' – ' . $str; | |
} | |
return $str; | |
} | |
echo date_interval(new DateTime('2015-02-25'), new DateTime('2015-02-25')) . "<br>\n"; | |
echo date_interval(new DateTime('2015-02-25'), new DateTime('2015-02-26')) . "<br>\n"; | |
echo date_interval(new DateTime('2015-02-25'), new DateTime('2015-03-26')) . "<br>\n"; | |
echo date_interval(new DateTime('2015-02-25'), new DateTime('2016-03-26')) . "<br>\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment