From f884a2e45ea928230342389eae7f2dc9cdbaa688 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Wed, 10 Oct 2012 22:09:37 -0400 Subject: [PATCH] Time class Left Date class for the time being for the time being, aliased the method over to the Time class. Added interval constants as well. Closes #6 --- classes/Date.php | 20 +------ classes/Time.php | 129 +++++++++++++++++++++++++++++++++++++++++ jar.php | 146 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 257 insertions(+), 38 deletions(-) create mode 100644 classes/Time.php diff --git a/classes/Date.php b/classes/Date.php index f70639c..41455ba 100644 --- a/classes/Date.php +++ b/classes/Date.php @@ -23,8 +23,6 @@ */ class Date { - // {{{ Age - /** * Age * @@ -36,24 +34,8 @@ class Date */ public static function age($date) { - if (!preg_match('/\d{4}-\d{2}-\d{2}/', $date)) - { - $date = date('Y-m-d', strtotime($date)); - } - - list($year, $month, $day) = explode('-', $date, 3); - - $age = date('Y') - $year; - - if (date('md') < $month . $day) - { - $age--; - } - - return $age; + return Time::age($date); } - - // }}} } ?> diff --git a/classes/Time.php b/classes/Time.php new file mode 100644 index 0000000..a2e7dc7 --- /dev/null +++ b/classes/Time.php @@ -0,0 +1,129 @@ + + * @copyright Copyright 2007-2012, Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @package PICKLES + * @link https://github.com/joshtronic/pickles + */ + +/** + * Time Class + * + * Just a simple collection of static functions to accomplish some of the more + * redundant time and date related manipulation. + */ +class Time +{ + // {{{ Intervals (in seconds) + + /** + * Minute + * + * Seconds in a minute + * + * @static + * @var integer + */ + public static $minute = 60; + + /** + * Hour + * + * Seconds in an hour (minute * 60) + * + * @static + * @var integer + */ + public static $hour = 3600; + + /** + * Day + * + * Seconds in a day (hour * 24) + * + * @static + * @var integer + */ + public static $day = 86400; + + /** + * Week + * + * Seconds in a week (day * 7) + * + * @static + * @var integer + */ + public static $week = 604800; + + /** + * Month + * + * Seconds in a month (day * 30) + * + * @static + * @var integer + */ + public static $month = 2592000; + + /** + * Quarter + * + * Seconds in a quarter (day * 90) + * + * @static + * @var integer + */ + public static $quarter = 7776000; + + /** + * Year + * + * Seconds in a year (day * 365) + * + * @static + * @var integer + */ + public static $year = 31536000; + + // }}} + + /** + * Age + * + * Calculates age based on the passed date. + * + * @static + * @param string $date birth / inception date + * @return integer $age number of years old + */ + public static function age($date) + { + if (!preg_match('/\d{4}-\d{2}-\d{2}/', $date)) + { + $date = date('Y-m-d', strtotime($date)); + } + + list($year, $month, $day) = explode('-', $date, 3); + + $age = date('Y') - $year; + + if (date('md') < $month . $day) + { + $age--; + } + + return $age; + } +} + +?> diff --git a/jar.php b/jar.php index d3aa5a5..6e7f197 100755 --- a/jar.php +++ b/jar.php @@ -2158,8 +2158,6 @@ class Database extends Object */ class Date { - // {{{ Age - /** * Age * @@ -2171,24 +2169,8 @@ class Date */ public static function age($date) { - if (!preg_match('/\d{4}-\d{2}-\d{2}/', $date)) - { - $date = date('Y-m-d', strtotime($date)); - } - - list($year, $month, $day) = explode('-', $date, 3); - - $age = date('Y') - $year; - - if (date('md') < $month . $day) - { - $age--; - } - - return $age; + return Time::age($date); } - - // }}} } /** @@ -6872,4 +6854,130 @@ class String // }}} } +/** + * Time Utility Collection + * + * PHP version 5 + * + * Licensed under The MIT License + * Redistribution of these files must retain the above copyright notice. + * + * @author Josh Sherman + * @copyright Copyright 2007-2012, Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @package PICKLES + * @link https://github.com/joshtronic/pickles + */ + +/** + * Time Class + * + * Just a simple collection of static functions to accomplish some of the more + * redundant time and date related manipulation. + */ +class Time +{ + // {{{ Intervals (in seconds) + + /** + * Minute + * + * Seconds in a minute + * + * @static + * @var integer + */ + public static $minute = 60; + + /** + * Hour + * + * Seconds in an hour (minute * 60) + * + * @static + * @var integer + */ + public static $hour = 3600; + + /** + * Day + * + * Seconds in a day (hour * 24) + * + * @static + * @var integer + */ + public static $day = 86400; + + /** + * Week + * + * Seconds in a week (day * 7) + * + * @static + * @var integer + */ + public static $week = 604800; + + /** + * Month + * + * Seconds in a month (day * 30) + * + * @static + * @var integer + */ + public static $month = 2592000; + + /** + * Quarter + * + * Seconds in a quarter (day * 90) + * + * @static + * @var integer + */ + public static $quarter = 7776000; + + /** + * Year + * + * Seconds in a year (day * 365) + * + * @static + * @var integer + */ + public static $year = 31536000; + + // }}} + + /** + * Age + * + * Calculates age based on the passed date. + * + * @static + * @param string $date birth / inception date + * @return integer $age number of years old + */ + public static function age($date) + { + if (!preg_match('/\d{4}-\d{2}-\d{2}/', $date)) + { + $date = date('Y-m-d', strtotime($date)); + } + + list($year, $month, $day) = explode('-', $date, 3); + + $age = date('Y') - $year; + + if (date('md') < $month . $day) + { + $age--; + } + + return $age; + } +} + ?>