2005-05-15 21:41:38 +00:00
|
|
|
<?PHP
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// + $Id$
|
|
|
|
// +------------------------------------------------------------------+ //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Cake <https://developers.nextco.com/cake/> + //
|
2005-05-17 21:27:56 +00:00
|
|
|
// + Copyright: (c) 2005, Cake Authors/Developers + //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Author(s): Michal Tatarynowicz aka Pies <tatarynowicz@gmail.com> + //
|
|
|
|
// + Larry E. Masters aka PhpNut <nut@phpnut.com> + //
|
|
|
|
// + Kamil Dzielinski aka Brego <brego.dk@gmail.com> + //
|
2005-05-15 21:41:38 +00:00
|
|
|
// +------------------------------------------------------------------+ //
|
2005-05-16 00:52:42 +00:00
|
|
|
// + Licensed under The MIT License + //
|
|
|
|
// + Redistributions of files must retain the above copyright notice. + //
|
2005-05-17 21:27:56 +00:00
|
|
|
// + See: http://www.opensource.org/licenses/mit-license.php + //
|
2005-05-15 21:41:38 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Purpose: Time
|
|
|
|
* Time related functions, formatting for dates etc.
|
|
|
|
*
|
|
|
|
* @filesource
|
2005-05-17 21:27:56 +00:00
|
|
|
* @author Cake Authors/Developers
|
2005-05-16 00:52:42 +00:00
|
|
|
* @copyright Copyright (c) 2005, Cake Authors/Developers
|
|
|
|
* @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers
|
2005-05-15 21:41:38 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
* @version $Revision$
|
2005-05-16 00:52:42 +00:00
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
uses ('object');
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Time related functions, formatting for dates etc.
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.libs
|
|
|
|
* @since Cake v 0.2.9
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class Time extends Object {
|
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Returns a formatted date string for given Datetime string.
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $date_string Datetime string
|
|
|
|
* @return string Formatted date string
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function nice ($date_string=null) {
|
|
|
|
$date = $date_string? strtotime($date_string): time();
|
|
|
|
return date("D, M jS Y, H:i", $date);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Returns a formatted descriptive date string for given datetime string.
|
|
|
|
* If the given date is today, the returned string could be "Today, 16:54".
|
|
|
|
* If the given date was yesterday, the returned string could be "Yesterday, 16:54".
|
|
|
|
* If $date_string's year is the current year, the returned string does not
|
|
|
|
* include mention of the year.
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
2005-05-31 23:18:22 +00:00
|
|
|
* @param string $date_string Datetime string
|
2005-06-02 19:37:06 +00:00
|
|
|
* @return string Described, relative date string
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function niceShort ($date_string=null) {
|
|
|
|
$date = $date_string? Time::fromString($date_string): time();
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
$y = Time::isThisYear($date)? '': ' Y';
|
2005-05-15 21:41:38 +00:00
|
|
|
|
2005-05-16 23:14:37 +00:00
|
|
|
if (Time::isToday($date))
|
|
|
|
return "Today, ".date("H:i", $date);
|
|
|
|
elseif (Time::wasYesterday($date))
|
|
|
|
return "Yesterday, ".date("H:i", $date);
|
|
|
|
else
|
|
|
|
return date("M jS{$y}, H:i", $date);
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns true if given datetime string is today.
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
2005-05-31 23:18:22 +00:00
|
|
|
* @param string $date Datetime string
|
|
|
|
* @return boolean True if datetime string is today
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function isToday ($date) {
|
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', time());
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns true if given datetime string is within current year.
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
2005-05-31 23:18:22 +00:00
|
|
|
* @param string $date Datetime string
|
|
|
|
* @return boolean True if datetime string is within current year
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function isThisYear ($date) {
|
|
|
|
return date('Y', $date) == date('Y', time());
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
2005-05-31 23:18:22 +00:00
|
|
|
* Returns true if given datetime string was yesterday.
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
2005-05-31 23:18:22 +00:00
|
|
|
* @param string $date Datetime string
|
|
|
|
* @return boolean True if datetime string was yesterday
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function wasYesterday ($date) {
|
|
|
|
return date('Y-m-d', $date) == date('Y-m-d', strtotime('yesterday'));
|
|
|
|
}
|
2005-05-15 21:41:38 +00:00
|
|
|
|
|
|
|
/**
|
2005-06-11 03:45:31 +00:00
|
|
|
* Returns a Unix timestamp from a textual datetime description. Wrapper for PHP function strtotime().
|
2005-05-15 21:41:38 +00:00
|
|
|
*
|
2005-05-31 23:18:22 +00:00
|
|
|
* @param string $date_string Datetime string to be represented as a Unix timestamp
|
|
|
|
* @return int Unix timestamp
|
2005-05-15 21:41:38 +00:00
|
|
|
*/
|
2005-05-16 23:14:37 +00:00
|
|
|
function fromString ($date_string) {
|
|
|
|
return strtotime($date_string);
|
|
|
|
}
|
2005-05-29 19:43:59 +00:00
|
|
|
|
|
|
|
/**
|
2005-06-02 19:37:06 +00:00
|
|
|
* Returns a date formatted for Atom RSS feeds.
|
2005-05-29 19:43:59 +00:00
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param string $date Datetime string
|
|
|
|
* @return string Formatted date string
|
2005-05-29 19:43:59 +00:00
|
|
|
*/
|
2005-06-11 03:45:31 +00:00
|
|
|
function toAtom ($date) {
|
|
|
|
return date('Y-m-d\TH:i:s\Z', $date);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats date for RSS feeds
|
|
|
|
*
|
|
|
|
* @param datetime $date Datetime string
|
|
|
|
* @return string Formatted date string
|
|
|
|
*/
|
|
|
|
function toRSS ($date) {
|
|
|
|
return date('D, d M Y H:i:s O', $date);
|
2005-05-29 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-06-11 03:45:31 +00:00
|
|
|
* Returns either a relative date or a formatted date depending
|
|
|
|
* on the difference between the current time and given datetime.
|
|
|
|
* $datetime should be in a <i>strtotime</i>-parsable format like MySQL datetime.
|
2005-05-29 19:43:59 +00:00
|
|
|
*
|
|
|
|
* Relative dates look something like this:
|
|
|
|
* 3 weeks, 4 days ago
|
2005-05-31 23:18:22 +00:00
|
|
|
* 15 seconds ago
|
2005-05-29 19:43:59 +00:00
|
|
|
* Formatted dates look like this:
|
|
|
|
* on 02/18/2004
|
|
|
|
*
|
2005-06-11 03:45:31 +00:00
|
|
|
* The returned string includes 'ago' or 'on' and assumes you'll properly add a word
|
2005-05-29 19:43:59 +00:00
|
|
|
* like 'Posted ' before the function output.
|
|
|
|
*
|
2005-06-02 19:37:06 +00:00
|
|
|
* @param $datetime Time in strtotime-parsable format
|
2005-05-31 23:18:22 +00:00
|
|
|
* @return string Relative time string.
|
2005-05-29 19:43:59 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-11 03:45:31 +00:00
|
|
|
function timeAgoInWords ($datetime)
|
|
|
|
{
|
|
|
|
$in_seconds=strtotime($datetime);
|
|
|
|
$diff = time()-$in_seconds;
|
|
|
|
$months = floor($diff/2419200);
|
|
|
|
$diff -= $months*2419200;
|
|
|
|
$weeks = floor($diff/604800);
|
|
|
|
$diff -= $weeks*604800;
|
|
|
|
$days = floor($diff/86400);
|
|
|
|
$diff -= $days*86400;
|
|
|
|
$hours = floor($diff/3600);
|
|
|
|
$diff -= $hours*3600;
|
|
|
|
$minutes = floor($diff/60);
|
|
|
|
$diff -= $minutes*60;
|
|
|
|
$seconds = $diff;
|
2005-05-29 19:43:59 +00:00
|
|
|
|
2005-06-11 03:45:31 +00:00
|
|
|
if ($months>0) {
|
|
|
|
// over a month old, just show date (mm/dd/yyyy format)
|
|
|
|
return 'on '.date("j/n/Y", $in_seconds);
|
|
|
|
} else {
|
|
|
|
$relative_date='';
|
|
|
|
if ($weeks>0) {
|
|
|
|
// weeks and days
|
|
|
|
$relative_date .= ($relative_date?', ':'').$weeks.' week'.($weeks>1?'s':'');
|
|
|
|
$relative_date .= $days>0?($relative_date?', ':'').$days.' day'.($days>1?'s':''):'';
|
|
|
|
} elseif ($days>0) {
|
|
|
|
// days and hours
|
|
|
|
$relative_date .= ($relative_date?', ':'').$days.' day'.($days>1?'s':'');
|
|
|
|
$relative_date .= $hours>0?($relative_date?', ':'').$hours.' hour'.($hours>1?'s':''):'';
|
|
|
|
} elseif ($hours>0) {
|
|
|
|
// hours and minutes
|
|
|
|
$relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours>1?'s':'');
|
|
|
|
$relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':''):'';
|
|
|
|
} elseif ($minutes>0) {
|
|
|
|
// minutes only
|
|
|
|
$relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':'');
|
|
|
|
} else {
|
|
|
|
// seconds only
|
|
|
|
$relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds>1?'s':'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// show relative date and add proper verbiage
|
|
|
|
return $relative_date.' ago';
|
2005-05-29 19:43:59 +00:00
|
|
|
}
|
|
|
|
|
2005-05-15 21:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|