Added date utility class. Similar to the string class but for dates. Contains a method to calculate an age from a date.
This commit is contained in:
parent
b2504f5a6b
commit
07f495d789
1 changed files with 59 additions and 0 deletions
59
classes/Date.php
Normal file
59
classes/Date.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Date Utility Collection
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <josh@gravityblvd.com>
|
||||
* @copyright Copyright 2007-2011, Josh Sherman
|
||||
* @license http://www.opensource.org/licenses/mit-license.html
|
||||
* @package PICKLES
|
||||
* @link http://p.ickl.es
|
||||
*/
|
||||
|
||||
/**
|
||||
* Date Class
|
||||
*
|
||||
* Just a simple collection of static functions to accomplish some of the more
|
||||
* redundant date related manipulation.
|
||||
*/
|
||||
class Date
|
||||
{
|
||||
// {{{ Age
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
// }}}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue