2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-04-02 19:17:04 +02:00
|
|
|
* Represents a column in the database with the type 'Time'.
|
|
|
|
*
|
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Time extends DBField {
|
|
|
|
|
2008-10-17 01:19:39 +02:00
|
|
|
function setValue($value) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($value) {
|
2009-08-10 05:56:59 +02:00
|
|
|
if(preg_match( '/(\d{1,2})[:.](\d{2})([a|A|p|P|][m|M])/', $value, $match )) $this->TwelveHour( $match );
|
2007-07-19 12:40:28 +02:00
|
|
|
else $this->value = date('H:i:s', strtotime($value));
|
2009-04-21 00:32:13 +02:00
|
|
|
} else {
|
|
|
|
$value = null;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-04-21 00:32:13 +02:00
|
|
|
/**
|
|
|
|
* Return a user friendly format for time
|
|
|
|
* in a 12 hour format.
|
|
|
|
*
|
|
|
|
* @return string Time in 12 hour format
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Nice() {
|
2009-12-16 06:39:23 +01:00
|
|
|
if($this->value) return date('g:ia', strtotime($this->value));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-21 00:32:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a user friendly format for time
|
|
|
|
* in a 24 hour format.
|
|
|
|
*
|
|
|
|
* @return string Time in 24 hour format
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Nice24() {
|
2009-12-16 06:39:23 +01:00
|
|
|
if($this->value) return date('H:i', strtotime($this->value));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-21 00:32:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the time using a particular formatting string.
|
|
|
|
*
|
|
|
|
* @param string $format Format code string. e.g. "g:ia"
|
|
|
|
* @return string The date in the requested format
|
|
|
|
*/
|
|
|
|
function Format($format) {
|
|
|
|
if($this->value) return date($format, strtotime($this->value));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-17 03:07:26 +02:00
|
|
|
function TwelveHour( $parts ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$hour = $parts[1];
|
|
|
|
$min = $parts[2];
|
|
|
|
$half = $parts[3];
|
|
|
|
|
2009-08-10 05:56:59 +02:00
|
|
|
// the transmation should exclude 12:00pm ~ 12:59pm
|
|
|
|
$this->value = (( (strtolower($half) == 'pm') && $hour != '12') ? $hour + 12 : $hour ) .":$min:00";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function requireField() {
|
2009-09-30 00:01:59 +02:00
|
|
|
$parts=Array('datatype'=>'time', 'arrayValue'=>$this->arrayValue);
|
2008-11-23 02:01:03 +01:00
|
|
|
$values=Array('type'=>'time', 'parts'=>$parts);
|
|
|
|
DB::requireField($this->tableName, $this->name, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-06 04:43:46 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2009-04-21 00:32:13 +02:00
|
|
|
return new TimeField($this->name, $title);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-21 00:32:13 +02:00
|
|
|
?>
|