Source for file Timestamp.inc
Documentation is available at Timestamp.inc
require_once $config->get('sysRoot'). 'alpha/exceptions/AlphaException.inc';
require_once $config->get('sysRoot'). 'alpha/model/types/AlphaType.inc';
require_once $config->get('sysRoot'). 'alpha/model/types/AlphaTypeInterface.inc';
* The Timestamp complex data type
* @package alpha::model::types
* @author John Collins <dev@alphaframework.org>
* @version $Id: Timestamp.inc 1462 2011-12-08 21:06:18Z johnc $
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* The textual version of the day, e.g. Monday
* The validation rule (reg-ex) applied to Date values
* The error message returned for invalid values
private $helper= 'Not a valid timestamp value! A timestamp should be in the format YYYY-MM-DD HH:MM:SS.';
* @throws IllegalArguementException
if($config->get('sysDefaultDateTime') == 'now') {
$this->weekday= date('l');
if (preg_match($this->validationRule, $timestamp)) {
* Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format
* @param string $dateTime
* Setter for the timestamp value
* @throws IllegalArguementException
$valid = 'The year value '. $year. ' provided is invalid!';
if (!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
$valid = 'The month value '. $month. ' provided is invalid!';
if (!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
$valid = 'The day value '. $day. ' provided is invalid!';
if (!isset ($valid) && !checkdate($month, $day, $year))
$valid = 'The day value '. $year. '-'. $month. '-'. $day. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $hour) || !($hour >= 0 && $hour < 24))
$valid = 'The hour value '. $hour. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60))
$valid = 'The minute value '. $minute. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60))
$valid = 'The second value '. $second. ' provided is invalid!';
$this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
$this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
$this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
$this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
$this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
$unixTime = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
$this->weekday= date('l', $unixTime);
* Getter for the Timestamp value
return $this->year. '-'. $this->month. '-'. $this->day. ' '. $this->hour. ':'. $this->minute. ':'. $this->second;
* Return the value in UNIX timestamp format
return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
* Getter for the date part
return $this->year. '-'. $this->month. '-'. $this->day;
* Get the date value as a string in the format "DD/MM/YYYY"
return $this->day. '/'. $this->month. '/'. substr($this->year, 2, 2);
* Setter for the date part
* @throws IllegalArguementException
public function setDate($year, $month, $day) {
$valid = 'The year value '. $year. ' provided is invalid!';
if (!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
$valid = 'The month value '. $month. ' provided is invalid!';
if (!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
$valid = 'The day value '. $day. ' provided is invalid!';
if (!isset ($valid) && !checkdate($month, $day, $year))
$valid = 'The day value '. $year. '/'. $month. '/'. $day. ' provided is invalid!';
$this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
$this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
$unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
$this->weekday= date('l', $unixTime);
* Getter for the time part
return $this->hour. ':'. $this->minute. ':'. $this->second;
* Getter for the year part
* Getter for the month part
* Getter for the day part
* Get the textual weekday part, e.g. Monday
* Getter for the hour part
* Getter for the minute part
* Getter for the second part
* Setter for the time part
* @throws IllegalArguementException
function setTime($hour, $minute, $second) {
if(!isset ($valid) && !preg_match('/^[0-9]{2}$/', $hour) || !($hour >= 0 && $hour < 24))
$valid = 'The hour value '. $hour. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60))
$valid = 'The minute value '. $minute. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60))
$valid = 'The second value '. $second. ' provided is invalid!';
$this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
$this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
$this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
* Accepts a full date/time string in YYYY-mm-dd hh:ii:ss format
* @param string $dateTime
* @throws IllegalArguementException
if($dateTime == 'Please select' || $dateTime == '0000-00-00 00:00:00') {
$spilt_by_space = explode(" ", $dateTime);
if(isset ($spilt_by_space[0])) {
$date = $spilt_by_space[0];
if(isset ($spilt_by_space[1])) {
$time = $spilt_by_space[1];
$split_by_dash = explode("-", $date);
if(isset ($split_by_dash[0])) {
$year = $split_by_dash[0];
if(isset ($split_by_dash[1])) {
$month = $split_by_dash[1];
if(isset ($split_by_dash[2])) {
$day = $split_by_dash[2];
$split_by_colon = explode(":", $time);
if(isset ($split_by_colon[0])) {
$hour = $split_by_colon[0];
if(isset ($split_by_colon[1])) {
$minute = $split_by_colon[1];
if(isset ($split_by_colon[2])) {
$second = $split_by_colon[2];
$valid = 'The year value '. $year. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $month))
$valid = 'The month value '. $month. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $day))
$valid = 'The day value '. $day. ' provided is invalid!';
if(!isset ($valid) && !checkdate($month, $day, $year))
$valid = 'The day value '. $year. '/'. $month. '/'. $day. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $hour) || !($hour >= 0 && $hour < 24))
$valid = 'The hour value '. $hour. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60))
$valid = 'The minute value '. $minute. ' provided is invalid!';
if(!isset ($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60))
$valid = 'The second value '. $second. ' provided is invalid!';
$this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
$this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
$this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
$this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
$this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
$unixTime = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
$this->weekday= date('l', $unixTime);
* Get the validation rule
return $this->validationRule;
* Set the validation rule
$this->validationRule = $rule;
* Get the validation helper text
* Set the validation helper text
|