* @link http://iamkoa.net * @license http://www.opensource.org/licenses/bsd-license.php */ class Time { /** * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string. * * @param string $dateString Datetime string * @return string Formatted date string */ function fromString($dateString) { if (is_integer($dateString) || is_numeric($dateString)) { return intval($dateString); } else return strtotime($dateString); } /** * Returns true if given datetime string was yesterday. * * @param string $dateString Datetime string or Unix timestamp * @return boolean True if datetime string was yesterday */ function wasYesterday($dateString) { $date = $this->fromString($dateString); $ret = date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday')); return $ret; } /** * Returns true if given datetime string is today. * * @param string $dateString Datetime string or Unix timestamp * @return boolean True if datetime string is today */ function isToday($dateString) { $date = $this->fromString($dateString); $ret = date('Y-m-d', $date) == date('Y-m-d', time()); return $ret; } /** * Returns "morning", "evening" or "night" depending upon date. * @param string $dateString Datetime string or Unix timestamp * @param string $format PHP string format function - strtoupper, ucfirst, ucwords, etc * @return string "morning", "evening" or "night" */ function timeOfDay($dateString, $format = 'strtolower') { $date = $this->fromString($dateString); $ret = null; $now = date('H',$date); // morning - midnight through 11am if ($now >= 0 && $now <= 11) $ret = $format('morning'); // evening - 6pm through 8pm if ($now >= 18 && $now <= 20) $ret = $format('evening'); // night - 9pm through 11pm if ($now >= 21 && $now <= 23) $ret = $format('night'); return $ret; } /** * Returns the difference in seconds between two dates * @param string $then Datetime string or Unix timestamp * @param string $now Datetime string or Unix timestamp * @return int Number of seconds */ function dateDiff($then, $now = null) { $then = $this->fromString($then); // if $now is null, assume we mean today if (!$now) $now = strtotime('now'); $now = $this->fromString($now); // calculate the difference $diff = $now - $then; return $diff; } /** * Returns true if specified datetime was within the interval specified, else false. * * @param mixed $timeInterval the numeric value with space then time type. Example of valid types: 6 hours, 2 days, 1 minute. * @param int $userOffset User's offset from GMT (in hours) * @param mixed $dateString the datestring or unix timestamp to compare * @return bool */ function wasWithinLast($timeInterval, $dateString, $userOffset = null) { $tmp = str_replace(' ', '', $timeInterval); if (is_numeric($tmp)) { $timeInterval = $tmp.' days'; } $date = $this->fromString($dateString, $userOffset); $interval = $this->fromString('-'.$timeInterval); if ($date >= $interval && $date <= time()) { return true; } return false; } /** * Returns an English humanized date string based on MySQL datetime or UNIX timestamp. * * @param mixed $dateString the datestring or unix timestamp to compare * @param string $default_format the default date() parse; example: 'j M, g:i A' * @return mixed "26 July, 2:45 PM", or "Yesterday morning", or "28 minutes ago", etc */ function timeAgoInWords($dateString = null, $default_format = 'j M, g:i A') { $date = $this->fromString($dateString); // what humans like $human = null; $now = strtotime('now'); // if $first_date is larger, falsify if ($date > $now) return false; // humanize it if ($this->isToday($date)) { // calculate difference $diff = $this->dateDiff($date); // break it down into hours $hours = floor($diff/3600); $diff = $diff % 3600; if ($hours >= 1) { @$human_hr = $hours > 1 ? "$hours hours " : "$hours hour "; } // only show minutes if less than one hour if ($hours < 1) { $minutes = floor($diff/60); $diff = $diff % 60; if ($minutes > 0) { @$human_hr .= $minutes > 1 ? "$minutes minutes " : "$minutes minute "; } } // only show seconds if less than one minute if (isset($minutes) && $minutes < 1) { $seconds = $diff; if ($seconds > 0) { @$human_hr .= $seconds > 1 ? "$seconds seconds" : "$seconds second"; } } // if beyond 5 hours, display $this->timeOfDay() if ($hours >= 5) { $human = $this->timeOfDay($date) == null ? "Today" : "This "; $human .= $this->timeOfDay($date); } else { // was in the last 5 hours - display hours/minutes/seconds ago $human = $human_hr." ago"; } } else if ($this->wasYesterday($date)) { $human = $this->timeOfDay($date) == 'night' ? "Last " : "Yesterday "; $human .= $this->timeOfDay($date); } else if ($this->wasWithinLast('4 days', $date)) { $human = date('l', $date)." ".$this->timeOfDay($date); } else if ($this->wasWithinLast('10 days', $date)) { $diff = $this->dateDiff($date); $days = floor($diff/86400); $human = $days > 1 ? $days." days" : $days." day"; $human .= " ago"; } else { $human = date($default_format, $date); } return $human; } }