2007-08-18 04:33:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Represents a date-time field.
|
2009-04-02 19:17:04 +02:00
|
|
|
* The field currently supports New Zealand date format (DD/MM/YYYY),
|
|
|
|
* or an ISO 8601 formatted date and time (Y-m-d H:i:s).
|
|
|
|
* Alternatively you can set a timestamp that is evaluated through
|
|
|
|
* PHP's built-in date() and strtotime() function according to your system locale.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-18 02:10:02 +02:00
|
|
|
* For all computations involving the current date and time,
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* please use {@link SS_Datetime::now()} instead of PHP's built-in date() and time()
|
2009-06-18 02:10:02 +02:00
|
|
|
* methods. This ensures that all time-based computations are testable with mock dates
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* through {@link SS_Datetime::set_mock_now()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 01:58:09 +02:00
|
|
|
* Example definition via {@link DataObject::$db}:
|
|
|
|
* <code>
|
|
|
|
* static $db = array(
|
2010-10-19 06:53:11 +02:00
|
|
|
* "Expires" => "SS_Datetime",
|
2010-10-15 01:58:09 +02:00
|
|
|
* );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-02 19:17:04 +02:00
|
|
|
* @todo Add localization support, see http://open.silverstripe.com/ticket/2931
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
|
|
|
*/
|
2012-03-16 02:16:07 +01:00
|
|
|
class SS_Datetime extends Date implements TemplateGlobalProvider {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-09-04 05:49:22 +02:00
|
|
|
public function setValue($value, $record = null, $markChanged = true) {
|
2012-02-24 04:18:36 +01:00
|
|
|
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
|
|
|
|
// saved as NULL in database
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->value = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-18 23:24:33 +02:00
|
|
|
// Default to NZ date format - strtotime expects a US date
|
2012-02-27 22:14:02 +01:00
|
|
|
if(preg_match('#^([0-9]+)/([0-9]+)/([0-9]+)$#', $value, $parts)) {
|
2009-05-18 23:24:33 +02:00
|
|
|
$value = "$parts[2]/$parts[1]/$parts[3]";
|
2009-05-19 02:35:15 +02:00
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2009-05-19 02:35:15 +02:00
|
|
|
if(is_numeric($value)) {
|
|
|
|
$this->value = date('Y-m-d H:i:s', $value);
|
|
|
|
} elseif(is_string($value)) {
|
2012-05-15 17:36:24 +02:00
|
|
|
// $this->value = date('Y-m-d H:i:s', strtotime($value));
|
|
|
|
try{
|
|
|
|
$date = new DateTime($value);
|
|
|
|
$this->value = $date->Format('Y-m-d H:i:s');
|
|
|
|
return;
|
|
|
|
}catch(Exception $e){
|
|
|
|
$this->value = null;
|
|
|
|
return;
|
|
|
|
}
|
2009-05-19 02:35:15 +02:00
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-15 01:58:09 +02:00
|
|
|
/**
|
2014-02-10 23:57:30 +01:00
|
|
|
* Returns the date and time (in 12-hour format) using the format string 'd/m/Y g:ia' e.g. '31/01/2014 2:23pm'.
|
|
|
|
* @return string Formatted date and time.
|
2010-10-15 01:58:09 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('d/m/Y g:ia');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2014-02-10 23:57:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the date and time (in 24-hour format) using the format string 'd/m/Y H:i' e.g. '28/02/2014 13:32'.
|
|
|
|
* @return string Formatted date and time.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Nice24() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('d/m/Y H:i');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2014-02-10 23:57:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the date using the format string 'd/m/Y' e.g. '28/02/2014'.
|
|
|
|
* @return string Formatted date.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Date() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('d/m/Y');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2014-02-10 23:57:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the time in 12-hour format using the format string 'g:ia' e.g. '1:32pm'.
|
|
|
|
* @return string Formatted time.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Time() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('g:ia');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2014-02-10 23:57:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the time in 24-hour format using the format string 'H:i' e.g. '13:32'.
|
|
|
|
* @return string Formatted time.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Time24() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('H:i');
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
/**
|
|
|
|
* Return a date and time formatted as per a CMS user's settings.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-03-11 03:42:11 +01:00
|
|
|
* @param Member $member
|
|
|
|
* @return boolean | string A time and date pair formatted as per user-defined settings.
|
|
|
|
*/
|
|
|
|
public function FormatFromSettings($member = null) {
|
2014-08-15 08:53:05 +02:00
|
|
|
require_once 'Zend/Date.php';
|
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
if(!$member) {
|
|
|
|
if(!Member::currentUserID()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
$formatD = $member->getDateFormat();
|
|
|
|
$formatT = $member->getTimeFormat();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-01 06:03:10 +02:00
|
|
|
$zendDate = new Zend_Date($this->getValue(), 'y-MM-dd HH:mm:ss');
|
2014-03-11 03:42:11 +01:00
|
|
|
return $zendDate->toString($formatD).' '.$zendDate->toString($formatT);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function requireField() {
|
2009-09-30 00:01:41 +02:00
|
|
|
$parts=Array('datatype'=>'datetime', 'arrayValue'=>$this->arrayValue);
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$values=Array('type'=>'SS_Datetime', 'parts'=>$parts);
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::require_field($this->tableName, $this->name, $values);
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2014-02-10 23:57:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the url encoded date and time in ISO 6801 format using format
|
|
|
|
* string 'Y-m-d%20H:i:s' e.g. '2014-02-28%2013:32:22'.
|
|
|
|
*
|
|
|
|
* @return string Formatted date and time.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function URLDatetime() {
|
2012-05-15 17:36:24 +02:00
|
|
|
if($this->value) return $this->Format('Y-m-d%20H:i:s');
|
2007-10-29 01:55:52 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-14 00:20:41 +02:00
|
|
|
public function scaffoldFormField($title = null, $params = null) {
|
2013-04-08 09:38:50 +02:00
|
|
|
$field = DatetimeField::create($this->name, $title);
|
|
|
|
|
|
|
|
// Show formatting hints for better usability
|
|
|
|
$dateField = $field->getDateField();
|
|
|
|
$dateField->setDescription(sprintf(
|
|
|
|
_t('FormField.Example', 'e.g. %s', 'Example format'),
|
|
|
|
Convert::raw2xml(Zend_Date::now()->toString($dateField->getConfig('dateformat')))
|
|
|
|
));
|
|
|
|
$dateField->setAttribute('placeholder', $dateField->getConfig('dateformat'));
|
|
|
|
$timeField = $field->getTimeField();
|
|
|
|
$timeField->setDescription(sprintf(
|
|
|
|
_t('FormField.Example', 'e.g. %s', 'Example format'),
|
|
|
|
Convert::raw2xml(Zend_Date::now()->toString($timeField->getConfig('timeformat')))
|
|
|
|
));
|
|
|
|
$timeField->setAttribute('placeholder', $timeField->getConfig('timeformat'));
|
|
|
|
|
|
|
|
return $field;
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 02:10:02 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-18 02:10:02 +02:00
|
|
|
*/
|
|
|
|
protected static $mock_now = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 02:10:02 +02:00
|
|
|
/**
|
|
|
|
* Returns either the current system date as determined
|
|
|
|
* by date(), or a mocked date through {@link set_mock_now()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_Datetime
|
2009-06-18 02:10:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function now() {
|
2009-06-18 02:10:02 +02:00
|
|
|
if(self::$mock_now) {
|
|
|
|
return self::$mock_now;
|
|
|
|
} else {
|
2012-03-27 06:57:42 +02:00
|
|
|
return DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
|
2009-06-18 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 02:10:02 +02:00
|
|
|
/**
|
|
|
|
* Mock the system date temporarily, which is useful for time-based unit testing.
|
|
|
|
* Use {@link clear_mock_now()} to revert to the current system date.
|
|
|
|
* Caution: This sets a fixed date that doesn't increment with time.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param SS_Datetime|string $datetime Either in object format, or as a SS_Datetime compatible string.
|
2009-06-18 02:10:02 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_mock_now($datetime) {
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
if($datetime instanceof SS_Datetime) {
|
2009-06-18 02:10:02 +02:00
|
|
|
self::$mock_now = $datetime;
|
|
|
|
} elseif(is_string($datetime)) {
|
2012-03-27 06:57:42 +02:00
|
|
|
self::$mock_now = DBField::create_field('SS_Datetime', $datetime);
|
2009-06-18 02:10:02 +02:00
|
|
|
} else {
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
throw new Exception('SS_Datetime::set_mock_now(): Wrong format: ' . $datetime);
|
2009-06-18 02:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 02:10:02 +02:00
|
|
|
/**
|
|
|
|
* Clear any mocked date, which causes
|
|
|
|
* {@link Now()} to return the current system date.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function clear_mock_now() {
|
2009-06-18 02:10:02 +02:00
|
|
|
self::$mock_now = null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-16 02:16:07 +01:00
|
|
|
public static function get_template_global_variables() {
|
|
|
|
return array(
|
|
|
|
'Now' => array('method' => 'now', 'casting' => 'SS_Datetime'),
|
|
|
|
);
|
|
|
|
}
|
2007-08-18 04:33:28 +02:00
|
|
|
}
|
|
|
|
|