Created
April 4, 2013 15:18
-
-
Save sebacruz/5311301 to your computer and use it in GitHub Desktop.
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 | |
| /** | |
| * Return a singular or plural string based on the count provided | |
| * | |
| * @param string $singular The singular form | |
| * @param string $plural The plural form | |
| * @param string $count The count | |
| * @return string The appropriately translated string | |
| */ | |
| function plural($singular, $plural, $count) { | |
| return ($count == 1) ? $singular : $plural; | |
| } | |
| /** | |
| * time_diff function. | |
| * | |
| * Determines the difference between two dates. | |
| * | |
| * The difference is returned in a human readable format such as "1 hour", | |
| * "5 mins", "2 days", "1 hour and 32 seconds", depending on the precision level. | |
| * | |
| * You have 6 presicion levels, example since October 10, 1986: | |
| * - 1: "X years" | |
| * - 2: "X years and X months" | |
| * - 3: "X years, X months and X days" | |
| * - 4: "X years, X months, X days or X weeks and X hours" | |
| * - 5: "X years, X months, X days or X weeks, X hours and X minutes" | |
| * - 6: "X years, X months, X days or X weeks, X hours, X minutes and X seconds" | |
| * | |
| * @access public | |
| * @uses DateTime | |
| * @param string $from String in a format accepted by | |
| * {@link http://ca.php.net/strtotime strtotime()} | |
| * @param mixed $to (default: NULL) Date to end the time difference in a | |
| * format accepted by {@link http://ca.php.net/strtotime strtotime()} | |
| * @param int $precision (default: 1) The level of precision to return. | |
| * @return string Human readable time difference. | |
| */ | |
| function time_diff($from, $to = NULL, $precision = 1) { | |
| $from = new DateTime($from); | |
| $to = new DateTime($to); | |
| $interval = $from->diff($to); | |
| $suffix = !$interval->invert ? ' ago' : ' more'; | |
| $format = array(); | |
| if ($interval->y !== 0) { | |
| $format[] = plural('%y year', '%y years', $interval->y); | |
| } | |
| if ($interval->m !== 0) { | |
| $format[] = plural('%m month', '%m months', $interval->m); | |
| } | |
| if ($interval->d !== 0) { | |
| if ($interval->d % 7 === 0) { | |
| $weeks = floor($interval->d / 7); | |
| $str = plural('%s week', '%s weeks', $weeks); | |
| $format[] = sprintf($str, $weeks); | |
| } else { | |
| $format[] = plural('%d day', '%d days', $interval->d); | |
| } | |
| } | |
| if ($interval->h !== 0) { | |
| $format[] = plural('%h hour', '%h hours', $interval->h); | |
| } | |
| if ($interval->i !== 0) { | |
| $format[] = plural('%i minute', '%i minutes', $interval->i); | |
| } | |
| if ($interval->s !== 0) { | |
| if (!count($format)) { | |
| return 'less than a minute ago'; | |
| } else { | |
| $format[] = plural('%s second', '%s seconds', $interval->s); | |
| } | |
| } | |
| // Sets the precision | |
| if (count($format) > 1) { | |
| $format = array_slice($format, 0, $precision); | |
| $last = array_pop($format); | |
| $format = implode(', ', $format); | |
| $format .= ($format == '') ? $last : ' and ' . $last; | |
| } else { | |
| $format = array_pop($format); | |
| } | |
| return $interval->format($format . $suffix); | |
| } | |
| $to = date('r', strtotime('1 year, 1 month, 28 days, 30 minutes, 20 seconds ago')); | |
| $diff = time_diff($to, NULL, 6); | |
| var_dump($to, $diff); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment