From 4f8d65606778ba220e05df0206e032eca3bcc575 Mon Sep 17 00:00:00 2001 From: Matt Peel Date: Sun, 17 Feb 2008 22:21:39 +0000 Subject: [PATCH] Merged r49479 from branches/2.1.1-madebyme git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@49651 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/fieldtypes/Date.php | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/core/model/fieldtypes/Date.php b/core/model/fieldtypes/Date.php index 2bdecf08b..444388eae 100644 --- a/core/model/fieldtypes/Date.php +++ b/core/model/fieldtypes/Date.php @@ -139,6 +139,46 @@ class Date extends DBField { } } } + + /** + * Gets the time difference, but always returns it in a certain format + * @param string $format The format, could be one of these: + * 'seconds', 'minutes', 'hours', 'days', 'months', 'years'. + * + * @return string + */ + function TimeDiffIn($format) { + if($this->value) { + $ago = abs(time() - strtotime($this->value)); + + switch($format) { + case "seconds": + $span = $ago; + return ($span != 1) ? "{$span} seconds" : "{$span} second"; + break; + case "minutes": + $span = round($ago/60); + return ($span != 1) ? "{$span} minutes" : "{$span} minute"; + break; + case "hours": + $span = round($ago/3600); + return ($span != 1) ? "{$span} hours" : "{$span} hour"; + break; + case "days": + $span = round($ago/86400); + return ($span != 1) ? "{$span} days" : "{$span} day"; + break; + case "months": + $span = round($ago/86400/30); + return ($span != 1) ? "{$span} months" : "{$span} month"; + break; + case "years": + $span = round($ago/86400/365); + return ($span != 1) ? "{$span} years" : "{$span} year"; + break; + } + } + } function requireField() { DB::requireField($this->tableName, $this->name, "date");