Created
December 8, 2015 17:04
-
-
Save fedek6/c68a072e0fb17e3a4da5 to your computer and use it in GitHub Desktop.
Convert datetime to human readable representation (polish version).
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 | |
/** | |
* Convert datetime to human readable representation. | |
* Polish version. | |
* | |
* @link http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago | |
*/ | |
function time_elapsed_string_pl($datetime, $full = false) { | |
$now = new DateTime; | |
$ago = new DateTime($datetime); | |
$diff = $now->diff($ago); | |
$diff->w = floor($diff->d / 7); | |
$diff->d -= $diff->w * 7; | |
$string = array( | |
'y' => array( 'rok', 'lata', 'lat' ), | |
'm' => array( 'miesiąc', 'miesiące', 'miesięcy' ), | |
'w' => array( 'tydzień', 'tygodnie', 'tygodni' ), | |
'd' => array( 'dzień', 'dni' ), | |
'h' => array( 'godzina', 'godziny', 'godzin' ), | |
'i' => array( 'minuta', 'minuty', 'minut' ), | |
's' => array( 'sekunda', 'sekundy', 'sekund' ), | |
); | |
foreach ($string as $k => &$v) { | |
if ($diff->$k) { | |
if( $k !== 'd' ) { | |
if( $diff->$k == 1 ) { | |
$v = $diff->$k . ' ' . $v[0]; | |
} else { | |
$last_digit = intval( substr( $diff->$k, -1 ) ); | |
if( $last_digit == 2 || $last_digit == 3 || $last_digit == 4 ) { | |
$v = $diff->$k . ' ' . $v[1]; | |
} else { | |
$v = $diff->$k . ' ' . $v[2]; | |
} | |
} | |
} else { | |
$v = $diff->$k . ' ' . ($diff->$k > 1 ? $v[1] : $v[0]); | |
} | |
} else { | |
unset($string[$k]); | |
} | |
} | |
if (!$full) $string = array_slice($string, 0, 1); | |
return $string ? implode(', ', $string) . ' temu' : 'przed chwilą'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example it retruns "2 lata temu" for 2012-12-08 14:20:13.