Overview

Namespaces

  • PHP
  • PhpOptions
    • Types

Classes

  • AType
  • CharType
  • DatetimeType
  • DateType
  • DirectoryType
  • EmailType
  • EnumType
  • FileType
  • InifileType
  • IntegerType
  • RealType
  • SeriesType
  • StringType
  • TimeType
  • Types
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * PhpOptions
  5:  * @link https://github.com/masicek/PhpOptions
  6:  * @author Viktor Mašíček <viktor@masicek.net>
  7:  * @license "New" BSD License
  8:  */
  9: 
 10: namespace PhpOptions\Types;
 11: 
 12: require_once __DIR__ . '/AType.php';
 13: 
 14: /**
 15:  * Datetime type
 16:  *
 17:  * Format: YEAR(-|.)MONTH(-|.)DAY[ HOURS[(-|:)MINUTES[(-|:)SECONDS]][ HOURS_FORMAT]]
 18:  * YEAR = four-digit number
 19:  * MONTH = one-digit or two-digit number or short name (three character)
 20:  * DAY = one-digit or two-digit number
 21:  * HOURS = one-digit or two-digit number
 22:  * MINUTES = one-digit or two-digit number
 23:  * SECONDS = one-digit or two-digit number
 24:  * HOURS_FORMAT = hour format = (AM|am|A|a|PM|pm|P|p)
 25:  *
 26:  * @author Viktor Mašíček <viktor@masicek.net>
 27:  */
 28: class DatetimeType extends AType
 29: {
 30: 
 31:     /**
 32:      * Filtered value return as timestamp
 33:      *
 34:      * @var bool
 35:      */
 36:     private $timestamp = FALSE;
 37: 
 38: 
 39:     /**
 40:      * Set object
 41:      *
 42:      * @param array $setting Array of setting of object
 43:      */
 44:     public function __construct($settings = array())
 45:     {
 46:         parent::__construct($settings);
 47:         if ($this->settingsHasFlag('timestamp', $settings))
 48:         {
 49:             $this->timestamp = TRUE;
 50:         }
 51:     }
 52: 
 53: 
 54:     /**
 55:      * Check type of value.
 56:      *
 57:      * @param mixed $value Checked value
 58:      *
 59:      * @return bool
 60:      */
 61:     public function check($value)
 62:     {
 63:         $dateString = $this->getDatetimeString($value);
 64: 
 65:         $isDate = FALSE;
 66:         if ($dateString)
 67:         {
 68:             // check validation
 69:             try {
 70:                 $dateObj = new \DateTime($dateString);
 71:                 $isDate = ($dateObj) ? TRUE : FALSE;
 72:             } catch (\Exception $e) {
 73:                 $isDate = FALSE;
 74:             }
 75:         }
 76: 
 77:         return $isDate;
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Return modified value
 83:      *
 84:      * @param mixed $value Filtered value
 85:      *
 86:      * @return mixed
 87:      */
 88:     protected function useFilter($value)
 89:     {
 90:         $dateString = $this->getDatetimeString($value);
 91:         $date = FALSE;
 92:         if ($dateString)
 93:         {
 94:             $date = new \DateTime($dateString);
 95:             if ($this->timestamp)
 96:             {
 97:                 $date = $date->getTimestamp();
 98:             }
 99:         }
100:         return $date;
101:     }
102: 
103: 
104:     /**
105:      * Return input date formated for DateTime object.
106:      *
107:      * @param string $value
108:      *
109:      * @return string
110:      */
111:     private function getDatetimeString($value)
112:     {
113:         // parse value
114:         $match = preg_match('/^([0-9]{4})[-.]([0-9a-zA-Z]+)[-.]([0-9]{1,2})( +([0-9]{1,2})([-:]([0-9]{1,2})([-:]([0-9]{1,2}))?)?( *(AM|am|A|a|PM|pm|P|p))?)?$/', $value, $matches);
115:         $date = '';
116:         if ($match)
117:         {
118:             // prepare date string
119:             $year = $matches[1];
120:             $month = $this->complete($matches[2]);
121:             $day = $this->complete($matches[3]);
122:             $hours = $this->complete(isset($matches[5]) ? $matches[5] : '');
123:             $minutes = $this->complete(isset($matches[7]) ? $matches[7] : '');
124:             $seconds = $this->complete(isset($matches[9]) ? $matches[9] : '');
125:             $hoursFormat = isset($matches[11]) ? $matches[11] : '';
126:             if (strlen($hoursFormat) == 1)
127:             {
128:                 $hoursFormat = $hoursFormat . 'M';
129:             }
130:             $hoursFormat = strtoupper($hoursFormat);
131:             $date = $year . '-' . $month . '-' . $day . ' ' . $hours . ':' . $minutes . ':' . $seconds . $hoursFormat;
132:         }
133: 
134:         return $date;
135:     }
136: 
137: 
138:     /**
139:      * Add zero if input value have only one character
140:      * Return two zero '00' if input value have not any character
141:      *
142:      * @param string $value
143:      *
144:      * @return string
145:      */
146:     private function complete($value)
147:     {
148:         $length = strlen($value);
149:         if ($length == 1)
150:         {
151:             $value = '0' . $value;
152:         }
153:         elseif ($length == 0)
154:         {
155:             $value = '00';
156:         }
157: 
158:         return $value;
159:     }
160: 
161: 
162: }
163: 
PhpOptions API documentation generated by ApiGen.
Generated using the TokenReflection library.