Version: current

      1 <?php
      2 /**
      3  * Represents a date-time field.
      4  * The field currently supports New Zealand date format (DD/MM/YYYY),
      5  * or an ISO 8601 formatted date and time (Y-m-d H:i:s).
      6  * Alternatively you can set a timestamp that is evaluated through
      7  * PHP's built-in date() and strtotime() function according to your system locale.
      8  *
      9  * For all computations involving the current date and time,
     10  * please use {@link SS_Datetime::now()} instead of PHP's built-in date() and time()
     11  * methods. This ensures that all time-based computations are testable with mock dates
     12  * through {@link SS_Datetime::set_mock_now()}.
     13  *
     14  * @todo Add localization support, see http://open.silverstripe.com/ticket/2931
     15  *
     16  * @package sapphire
     17  * @subpackage model
     18  */
     19 class SS_Datetime extends Date {
     20
     21     function setValue($value) {
     22         // Default to NZ date format - strtotime expects a US date
     23         if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
     24             $value = "$parts[2]/$parts[1]/$parts[3]";
     25         }
     26
     27         if(is_numeric($value)) {
     28             $this->value = date('Y-m-d H:i:s', $value);
     29         } elseif(is_string($value)) {
     30             $this->value = date('Y-m-d H:i:s', strtotime($value));
     31         }
     32     }
     33
     34     function Nice() {
     35         return date('d/m/Y g:ia', strtotime($this->value));
     36     }
     37     function Nice24() {
     38         return date('d/m/Y H:i', strtotime($this->value));
     39     }
     40     function Date() {
     41         return date('d/m/Y', strtotime($this->value));
     42     }
     43     function Time() {
     44         return date('g:ia', strtotime($this->value));
     45     }
     46     function Time24() {
     47         return date('H:i', strtotime($this->value));
     48     }
     49
     50     function requireField() {
     51         $parts=Array('datatype'=>'datetime', 'arrayValue'=>$this->arrayValue);
     52         $values=Array('type'=>'SS_Datetime', 'parts'=>$parts);
     53         DB::requireField($this->tableName, $this->name, $values);
     54     }
     55
     56     function URLDatetime() {
     57         return date('Y-m-d%20H:i:s', strtotime($this->value));
     58     }
     59
     60     public function scaffoldFormField($title = null, $params = null) {
     61         return new DatetimeField($this->name, $title);
     62     }
     63
     64     /**
     65      *
     66      */
     67     protected static $mock_now = null;
     68
     69     /**
     70      * Returns either the current system date as determined
     71      * by date(), or a mocked date through {@link set_mock_now()}.
     72      *
     73      * @return SS_Datetime
     74      */
     75     static function now() {
     76         if(self::$mock_now) {
     77             return self::$mock_now;
     78         } else {
     79             return DBField::create('SS_Datetime', date('Y-m-d H:i:s'));
     80         }
     81     }
     82
     83     /**
     84      * Mock the system date temporarily, which is useful for time-based unit testing.
     85      * Use {@link clear_mock_now()} to revert to the current system date.
     86      * Caution: This sets a fixed date that doesn't increment with time.
     87      *
     88      * @param SS_Datetime|string $datetime Either in object format, or as a SS_Datetime compatible string.
     89      */
     90     static function set_mock_now($datetime) {
     91         if($datetime instanceof SS_Datetime) {
     92             self::$mock_now = $datetime;
     93         } elseif(is_string($datetime)) {
     94             self::$mock_now = DBField::create('SS_Datetime', $datetime);
     95         } else {
     96             throw new Exception('SS_Datetime::set_mock_now(): Wrong format: ' . $datetime);
     97         }
     98     }
     99
    100     /**
    101      * Clear any mocked date, which causes
    102      * {@link Now()} to return the current system date.
    103      */
    104     static function clear_mock_now() {
    105         self::$mock_now = null;
    106     }
    107 }
    108
    109 ?>

[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. -