Created
March 11, 2014 10:32
-
-
Save wpottier/9483176 to your computer and use it in GitHub Desktop.
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
$datetime_transformer = new \Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer(null, null, 'Y-m-d H:i:s'); | |
$timestamp_transformer = new \Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer(); | |
# Transforming to Timestamp | |
if (!($from_time instanceof \DateTime) && !is_numeric($from_time)) { | |
$from_time = $datetime_transformer->reverseTransform($from_time); | |
$from_time = $timestamp_transformer->transform($from_time); | |
} elseif($from_time instanceof \DateTime) { | |
$from_time = $timestamp_transformer->transform($from_time); | |
} | |
$to_time = empty($to_time) ? new \DateTime('now') : $to_time; | |
# Transforming to Timestamp | |
if (!($to_time instanceof \DateTime) && !is_numeric($to_time)) { | |
$to_time = $datetime_transformer->reverseTransform($to_time); | |
$to_time = $timestamp_transformer->transform($to_time); | |
} elseif($to_time instanceof \DateTime) { | |
$to_time = $timestamp_transformer->transform($to_time); | |
} | |
$distance_in_minutes = round((abs($to_time - $from_time))/60); | |
$distance_in_seconds = round(abs($to_time - $from_time)); | |
if ($distance_in_minutes <= 1){ | |
if ($include_seconds){ | |
if ($distance_in_seconds < 5){ | |
return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 5)); | |
} | |
elseif($distance_in_seconds < 10){ | |
return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 10)); | |
} | |
elseif($distance_in_seconds < 20){ | |
return $this->translator->trans('less than %seconds seconds ago', array('%seconds' => 20)); | |
} | |
elseif($distance_in_seconds < 40){ | |
return $this->translator->trans('half a minute ago'); | |
} | |
elseif($distance_in_seconds < 60){ | |
return $this->translator->trans('less than a minute ago'); | |
} | |
else { | |
return $this->translator->trans('1 minute ago'); | |
} | |
} | |
return ($distance_in_minutes===0) ? $this->translator->trans('less than a minute ago', array()) : $this->translator->trans('1 minute ago', array()); | |
} | |
elseif ($distance_in_minutes <= 45){ | |
return $this->translator->transchoice('%minutes minutes ago', $distance_in_minutes, array('%minutes' => $distance_in_minutes)); | |
} | |
elseif ($distance_in_minutes <= 90){ | |
return $this->translator->trans('about 1 hour ago'); | |
} | |
elseif ($distance_in_minutes <= 1440){ | |
return $this->translator->transchoice('about %hours hours ago', round($distance_in_minutes/60), array('%hours' => round($distance_in_minutes/60))); | |
} | |
elseif ($distance_in_minutes <= 2880){ | |
return $this->translator->trans('1 day ago'); | |
} | |
else{ | |
$distance_in_days = round($distance_in_minutes/1440); | |
if (!$include_months || $distance_in_days <= 30) { | |
return $this->translator->trans('%days days ago', array('%days' => round($distance_in_days))); | |
} | |
else { | |
return $this->translator->transchoice('{1} 1 month ago |]1,Inf[ %months months ago', round($distance_in_days/30), array('%months' => round($distance_in_days/30))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment