Gazelle/classes/time.class.php

192 lines
4.0 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
if (!extension_loaded('date')) {
2012-12-27 08:00:27 +00:00
error('Date extension not loaded.');
2011-03-28 14:21:28 +00:00
}
function time_ago($TimeStamp) {
2013-04-21 08:00:52 +00:00
if (!is_number($TimeStamp)) { // Assume that $TimeStamp is SQL timestamp
if ($TimeStamp == '0000-00-00 00:00:00') {
return false;
}
2011-03-28 14:21:28 +00:00
$TimeStamp = strtotime($TimeStamp);
}
2013-04-21 08:00:52 +00:00
if ($TimeStamp == 0) {
return false;
}
return time() - $TimeStamp;
2011-03-28 14:21:28 +00:00
}
2013-05-27 08:00:58 +00:00
function time_diff($TimeStamp, $Levels = 2, $Span = true, $Lowercase = false) {
2012-12-27 08:00:27 +00:00
/*
Returns a <span> by default but can optionally return the raw time
difference in text (e.g. "16 hours and 28 minutes", "1 day, 18 hours").
*/
2013-04-21 08:00:52 +00:00
if (!is_number($TimeStamp)) { // Assume that $TimeStamp is SQL timestamp
if ($TimeStamp == '0000-00-00 00:00:00') {
return 'Never';
}
2011-03-28 14:21:28 +00:00
$TimeStamp = strtotime($TimeStamp);
}
2013-04-21 08:00:52 +00:00
if ($TimeStamp == 0) {
return 'Never';
}
$Time = time() - $TimeStamp;
2013-02-22 08:00:24 +00:00
2012-12-27 08:00:27 +00:00
// If the time is negative, then it expires in the future.
2013-04-21 08:00:52 +00:00
if ($Time < 0) {
2011-03-28 14:21:28 +00:00
$Time = -$Time;
$HideAgo = true;
}
2013-04-21 08:00:52 +00:00
$Years = floor($Time / 31556926); // seconds in one year
$Remain = $Time - $Years * 31556926;
2011-03-28 14:21:28 +00:00
2013-04-21 08:00:52 +00:00
$Months = floor($Remain / 2629744); // seconds in one month
$Remain = $Remain - $Months * 2629744;
2011-03-28 14:21:28 +00:00
2013-04-21 08:00:52 +00:00
$Weeks = floor($Remain / 604800); // seconds in one week
$Remain = $Remain - $Weeks * 604800;
2011-03-28 14:21:28 +00:00
2013-04-21 08:00:52 +00:00
$Days = floor($Remain / 86400); // seconds in one day
$Remain = $Remain - $Days * 86400;
2011-03-28 14:21:28 +00:00
2013-04-21 08:00:52 +00:00
$Hours=floor($Remain / 3600); // seconds in one hour
$Remain = $Remain - $Hours * 3600;
2011-03-28 14:21:28 +00:00
2013-04-21 08:00:52 +00:00
$Minutes = floor($Remain / 60); // seconds in one minute
$Remain = $Remain - $Minutes * 60;
2011-03-28 14:21:28 +00:00
2013-04-21 08:00:52 +00:00
$Seconds = $Remain;
2011-03-28 14:21:28 +00:00
$Return = '';
2013-04-21 08:00:52 +00:00
if ($Years > 0 && $Levels > 0) {
if ($Years > 1) {
2011-03-28 14:21:28 +00:00
$Return .= $Years.' years';
} else {
$Return .= $Years.' year';
}
$Levels--;
}
2013-04-21 08:00:52 +00:00
if ($Months > 0 && $Levels > 0) {
if ($Return != '') {
2011-03-28 14:21:28 +00:00
$Return.=', ';
}
2013-04-21 08:00:52 +00:00
if ($Months > 1) {
2011-03-28 14:21:28 +00:00
$Return.=$Months.' months';
} else {
$Return.=$Months.' month';
}
$Levels--;
}
2013-04-21 08:00:52 +00:00
if ($Weeks > 0 && $Levels > 0) {
if ($Return != '') {
2011-03-28 14:21:28 +00:00
$Return.=', ';
}
2013-02-22 08:00:24 +00:00
if ($Weeks>1) {
2011-03-28 14:21:28 +00:00
$Return.=$Weeks.' weeks';
} else {
$Return.=$Weeks.' week';
}
$Levels--;
}
2013-04-21 08:00:52 +00:00
if ($Days > 0 && $Levels > 0) {
if ($Return != '') {
2011-03-28 14:21:28 +00:00
$Return.=', ';
}
2013-04-21 08:00:52 +00:00
if ($Days > 1) {
2011-03-28 14:21:28 +00:00
$Return.=$Days.' days';
} else {
$Return.=$Days.' day';
}
$Levels--;
}
2013-04-21 08:00:52 +00:00
if ($Hours > 0 && $Levels > 0) {
if ($Return != '') {
2011-03-28 14:21:28 +00:00
$Return.=', ';
}
2013-04-21 08:00:52 +00:00
if ($Hours > 1) {
2011-03-28 14:21:28 +00:00
$Return.=$Hours.' hours';
} else {
$Return.=$Hours.' hour';
}
$Levels--;
}
2013-04-21 08:00:52 +00:00
if ($Minutes > 0 && $Levels > 0) {
if ($Return != '') {
2011-03-28 14:21:28 +00:00
$Return.=' and ';
}
2013-04-30 18:18:07 +00:00
if ($Minutes > 1) {
2011-03-28 14:21:28 +00:00
$Return.=$Minutes.' mins';
} else {
$Return.=$Minutes.' min';
}
$Levels--;
}
2013-02-22 08:00:24 +00:00
2013-04-21 08:00:52 +00:00
if ($Return == '') {
2011-03-28 14:21:28 +00:00
$Return = 'Just now';
} elseif (!isset($HideAgo)) {
$Return .= ' ago';
}
if ($Lowercase) {
$Return = strtolower($Return);
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
if ($Span) {
return '<span class="time" title="'.date('M d Y, H:i', $TimeStamp).'">'.$Return.'</span>';
} else {
return $Return;
}
}
/* SQL utility functions */
function time_plus($Offset) {
2013-04-21 08:00:52 +00:00
return date('Y-m-d H:i:s', time() + $Offset);
2011-03-28 14:21:28 +00:00
}
function time_minus($Offset, $Fuzzy = false) {
2013-04-21 08:00:52 +00:00
if ($Fuzzy) {
return date('Y-m-d 00:00:00', time() - $Offset);
2011-03-28 14:21:28 +00:00
} else {
2013-04-21 08:00:52 +00:00
return date('Y-m-d H:i:s', time() - $Offset);
2011-03-28 14:21:28 +00:00
}
}
function sqltime($timestamp = false) {
if ($timestamp === false) {
$timestamp = time();
}
return date('Y-m-d H:i:s', $timestamp);
2011-03-28 14:21:28 +00:00
}
function validDate($DateString) {
2013-04-21 08:00:52 +00:00
$DateTime = explode(' ', $DateString);
if (count($DateTime) != 2) {
return false;
}
2011-03-28 14:21:28 +00:00
list($Date, $Time) = $DateTime;
2013-04-21 08:00:52 +00:00
$SplitTime = explode(':', $Time);
if (count($SplitTime) != 3) {
return false;
}
2011-03-28 14:21:28 +00:00
list($H, $M, $S) = $SplitTime;
2013-05-27 08:00:58 +00:00
if ($H != 0 && !(is_number($H) && $H < 24 && $H >= 0)) { return false; }
if ($M != 0 && !(is_number($M) && $M < 60 && $M >= 0)) { return false; }
if ($S != 0 && !(is_number($S) && $S < 60 && $S >= 0)) { return false; }
2013-04-21 08:00:52 +00:00
$SplitDate = explode('-', $Date);
if (count($SplitDate) != 3) {
return false;
}
2011-03-28 14:21:28 +00:00
list($Y, $M, $D) = $SplitDate;
return checkDate($M, $D, $Y);
}
?>