Version: current

     1 <?php
     2 /**
     3  * Represents a column in the database with the type 'Time'.
     4  *
     5  * @todo Add localization support, see http://open.silverstripe.com/ticket/2931
     6  *
     7  * @package sapphire
     8  * @subpackage model
     9  */
    10 class Time extends DBField {
    11
    12     function setValue($value) {
    13         if($value) {
    14             if(preg_match( '/(\d{1,2})[:.](\d{2})([a|A|p|P|][m|M])/', $value, $match )) $this->TwelveHour( $match );
    15             else $this->value = date('H:i:s', strtotime($value));
    16         } else {
    17             $value = null;
    18         }
    19     }
    20
    21     /**
    22      * Return a user friendly format for time
    23      * in a 12 hour format.
    24      *
    25      * @return string Time in 12 hour format
    26      */
    27     function Nice() {
    28         if($this->value) return date('g:ia', strtotime($this->value));
    29     }
    30
    31     /**
    32      * Return a user friendly format for time
    33      * in a 24 hour format.
    34      *
    35      * @return string Time in 24 hour format
    36      */
    37     function Nice24() {
    38         if($this->value) return date('H:i', strtotime($this->value));
    39     }
    40
    41     /**
    42      * Return the time using a particular formatting string.
    43      *
    44      * @param string $format Format code string. e.g. "g:ia"
    45      * @return string The date in the requested format
    46      */
    47     function Format($format) {
    48         if($this->value) return date($format, strtotime($this->value));
    49     }
    50
    51     function TwelveHour( $parts ) {
    52         $hour = $parts[1];
    53         $min = $parts[2];
    54         $half = $parts[3];
    55
    56         // the transmation should exclude 12:00pm ~ 12:59pm
    57         $this->value = (( (strtolower($half) == 'pm') && $hour != '12') ? $hour + 12 : $hour ) .":$min:00";
    58     }
    59
    60     function requireField() {
    61         $parts=Array('datatype'=>'time', 'arrayValue'=>$this->arrayValue);
    62         $values=Array('type'=>'time', 'parts'=>$parts);
    63         DB::requireField($this->tableName, $this->name, $values);
    64     }
    65
    66     public function scaffoldFormField($title = null, $params = null) {
    67         return new TimeField($this->name, $title);
    68     }
    69
    70 }
    71 ?>

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