1 <?php
2 3 4 5 6 7 8 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 23 24 25 26
27 function Nice() {
28 if($this->value) return date('g:ia', strtotime($this->value));
29 }
30
31 32 33 34 35 36
37 function Nice24() {
38 if($this->value) return date('H:i', strtotime($this->value));
39 }
40
41 42 43 44 45 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
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.
-