Version: current

     1 <?php
     2
     3 /*
     4  * This file is part of the sfDateTimePlugin package.
     5  * (c) 2007 Stephen Riesenberg <sjohnr@gmail.com>
     6  *
     7  * For the full copyright and license information, please view the LICENSE
     8  * file that was distributed with this source code.
     9  */
    10
    11 /**
    12  *
    13  * sfDateTimeToolkit class.
    14  *
    15  * A toolkit for the sfDateTimePlugin.
    16  *
    17  * @package sfDateTimePlugin
    18  * @author  Stephen Riesenberg <sjohnr@gmail.com>
    19  * @version SVN: $Id$
    20  */
    21 class sfDateTimeToolkit
    22 {
    23     /**
    24      * Breaks down the individual components of the timestamp.
    25      *
    26      * @param   timestamp
    27      * @return  array
    28      */
    29     public static function breakdown($ts = null)
    30     {
    31         // default to now
    32         if ($ts === null) $ts = sfDateTimeToolkit::now();
    33
    34         // gather individual variables
    35         $H = date('H', $ts); // hour
    36         $i = date('i', $ts); // minute
    37         $s = date('s', $ts); // second
    38         $m = date('m', $ts); // month
    39         $d = date('d', $ts); // day
    40         $Y = date('Y', $ts); // year
    41
    42         return array($H, $i, $s, $m, $d, $Y);
    43     }
    44
    45     /**
    46      * Returns the current timestamp.
    47      *
    48      * @return  timestamp
    49      *
    50      * @see     time
    51      */
    52     public static function now()
    53     {
    54         return time();
    55     }
    56
    57     /**
    58      * Retrieve the timestamp from a number of different formats.
    59      *
    60      * @param   mixed   value to use for timestamp retrieval
    61      */
    62     public static function getTS($value = null)
    63     {
    64         if ($value === null)
    65         {
    66             return sfDateTimeToolkit::now();
    67         }
    68         else if ($value instanceof sfDate)
    69         {
    70             return $value->get();
    71         }
    72         else if (!is_numeric($value))
    73         {
    74             return strtotime($value);
    75         }
    76         else if (is_numeric($value))
    77         {
    78             return $value;
    79         }
    80
    81         throw new sfDateTimeException(sprintf('A timestamp could not be retrieved from the value: %s', $value));
    82     }
    83 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -