Skip to content

Instantly share code, notes, and snippets.

@fedek6
Created December 8, 2015 17:04
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 fedek6/c68a072e0fb17e3a4da5 to your computer and use it in GitHub Desktop.
Save fedek6/c68a072e0fb17e3a4da5 to your computer and use it in GitHub Desktop.
Convert datetime to human readable representation (polish version).
<?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ą';
}
?>
@fedek6
Copy link
Author

fedek6 commented Dec 8, 2015

For example it retruns "2 lata temu" for 2012-12-08 14:20:13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment