Permalink
Browse files

datetime formatting fixes for unknown dates. fixes #83

  • Loading branch information...
1 parent 711db6d commit b5b4ccd367bec72f675d48fc6383d934f6089c02 Zach Hoeken committed Jul 25, 2013
Showing with 32 additions and 16 deletions.
  1. +32 −16 classes/utility.php
View
48 classes/utility.php
@@ -254,30 +254,35 @@ public static function dayOfWeek($numeric_day)
public function newformatDate($date)
{
- $time = strtotime($date);
- return date("F j, Y, G:i", $time);
-
+ if (strtotime($date) < 0)
+ return 'unknown';
+ else
+ return date("F j, Y, G:i", strtotime($date));
}
public function formatDate($date)
{
- $time = strtotime($date);
-
- //if (abs(time() - $time) > 86400)
- return date("M j, Y", $time);
- //else
- // return date("g:i a", $time);
+ if (strtotime($date) < 0)
+ return 'unknown';
+ else
+ return date("M j, Y", strtotime($date));
}
public function formatDateTime($date)
{
- return date("M j, Y // G:i", strtotime($date));
+ if (strtotime($date) < 0)
+ return 'unknown';
+ else
+ return date("M j, Y // G:i", strtotime($date));
}
public static function W3CDate($datetime)
{
- return date("Y-m-d\TH:i:sP", $datetime);
+ if (strtotime($datetime) < 0)
+ return 'unknown';
+ else
+ return date("Y-m-d\TH:i:sP", $datetime);
}
public static function relativeDays($datetime)
@@ -286,6 +291,9 @@ public static function relativeDays($datetime)
$elapsed = time() - strtotime($datetime);
$days = floor(abs($elapsed / Cache::TIME_ONE_DAY));
+ if (strtotime($datetime) < 0)
+ return 'unknown';
+
//special formatting...
if ($days == 0)
return "today";
@@ -342,7 +350,7 @@ public static function getHours($datediff)
public static function getTimeAgo($datetime)
{
if (trim($datetime) == "" || trim($datetime) == "0000-00-00 00:00:00")
- return false;
+ return 'unknown';
$datediff = strtotime(date("Y-m-d H:i:s")) - strtotime($datetime);
@@ -354,7 +362,9 @@ public static function getTimeAgo($datetime)
//echo "$min $hours $days $months $years ";
- if ($datediff < 60) { // seconds
+ if (strtotime($datetime) < 0) {
+ return 'unknown';
+ } else if ($datediff < 60) { // seconds
if ($datediff == 0) return "just now";
return "$datediff second".self::pluralizer($datediff>1)." ago";
} else if ($min < 60) {
@@ -375,12 +385,15 @@ public static function getTimeAgo($datetime)
public static function relativeTime($datetime)
{
if (trim($datetime) == "")
- return false;
+ return 'unknown';
$from = strtotime(date("Y-m-d H:i:s"));
$to = strtotime($datetime);
$datediff = $from - $to;
-
+
+ if ($to < 0)
+ return 'unknown';
+
$min = round(abs($datediff) / (60));
$hours = round(abs($datediff) / (60 * 60));
$days = round(abs($datediff) / (60 * 60 * 24));
@@ -430,7 +443,7 @@ public static function relativeTime($datetime)
public static function relativeDate($datetime)
{
if (trim($datetime) == "")
- return false;
+ return 'unknown';
$from = strtotime(date("Y-m-d"));
$to = strtotime($datetime);
@@ -444,6 +457,9 @@ public static function relativeDate($datetime)
// Utility::log("$datetime datediff = $min $hours $days $months $years ");
+ if ($to < 0)
+ return 'unknown';
+
if ($datediff >= 0)
{
if ($days == 0)

0 comments on commit b5b4ccd

Please sign in to comment.