Xataface 2.0
Xataface Application Framework
Dataface/converters/date.php
Go to the documentation of this file.
00001 <?php
00002 /*-------------------------------------------------------------------------------
00003  * Xataface Web Application Framework
00004  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *-------------------------------------------------------------------------------
00020  */
00029 class Dataface_converters_date {
00030 
00035         static function qf2UnixTimestamp($value){
00036                 $date = Dataface_converters_date::qf2Table($value);
00037                 $timestamp = strtotime( Dataface_converters_date::datetime_to_string($date) );
00038                 return $timestamp;
00039         }
00040         
00045         static function qf2Table($value){
00046                 $out = array();
00047                 foreach ($value as $key=>$val){
00048                         if ( is_array($val) ) $out[$key] = $val[0];
00049                         else $out[$key] = $val;
00050                 }
00051                 return Dataface_converters_date::parseDate($out);
00052         }
00053 
00054         
00059         static function parseDate($value){
00060                 if ( !isset($value) || !$value ) return null;
00061                 if ( $value == '0000-00-00' || $value == '0000-00-00 00:00:00' ) return null;
00062                 if ( is_array($value) and (isset( $value['year']) or isset($value['hours'])) ) return $value;
00063                         // if it is already in the correct format, we don't need to parse it.
00064                 
00065                 if ( Dataface_converters_date::isTimeStamp($value) ) {
00066                         $date = array();
00067                         if ( strlen($value)>=4) $date['Y'] = substr($value,0,4);
00068                         if ( strlen($value)>=6) $date['m'] = substr($value,4,2);
00069                         if ( strlen($value)>=8) $date['d'] = substr($value,6,2);
00070                         if ( strlen($value)>=10) $date['H'] = substr($value,8,2);
00071                         if ( strlen($value)>=12) $date['i'] = substr($value,10,2);
00072                         if ( strlen($value)>=14) $date['s'] = substr($value,12,2);
00073                 }
00074                 else if ( !is_array($value) ){
00075                         if ( function_exists('date_parse') ){
00076                                 $out = date_parse($value);
00077                                 $out['hours'] = $out['hour'];
00078                                 unset($out['hour']);
00079                                 $out['minutes'] = $out['minute'];
00080                                 unset($out['minute']);
00081                                 $out['seconds'] = $out['second'];
00082                                 unset($out['second']);
00083                                 return $out;
00084                         } else if ( Dataface_converters_date::inRange($value) ){
00085                                 // strtotime cannot seem to calculate the time properly on this
00086                                 // so we will manually parse it;
00087                                 if ( preg_match('/^(\d{4})(-(\d{2}))?(-(\d{2}))?( (\d{2}):(\d{2})(:(\d{2}))?)?$/', $value, $matches)){
00088                                         $date = array();
00089                                         $date['year'] = $matches[1];
00090                                         $date['month'] = @$matches[3];
00091                                         $date['day'] = @$matches[5];
00092                                         $date['hours'] = @$matches[7];
00093                                         $date['minutes'] = @$matches[8];
00094                                         $date['seconds'] = @$matches[10];
00095                                         return $date;
00096                                 }
00097                                 
00098                         }
00099                         $isNull = true;
00100                         $units = explode(' ','Y m M F d h a A i s');
00101                         $date = array();
00102                         foreach ($units as $unit){
00103                                 if ( $value ){
00104                                         $date[$unit] = date($unit, strtotime($value));
00105                                         $isNull = false;
00106                                 } else {
00107                                         $date[$unit] = null; //date($unit);
00108                                 }
00109                         }
00110                         if ( $isNull ) return null;
00111                         
00112                 
00113                 } else {
00114                         $date = $value;
00115                 }
00116                 $params = array();
00117                 $params['year'] = isset($date['Y']) ? $date['Y'] : date('Y');
00118                 $params['month'] = isset($date['m']) ? $date['m'] : (
00119                                                                         isset($date['M']) ? $date['M'] : (
00120                                                                         isset($date['F']) ? $date['F'] : null//date('m')
00121                                                                         ));
00122                 $params['day'] = isset($date['d']) ? $date['d'] : null;//date('d');
00123                 if ( isset($date['H'] ) ) $params['hours'] = $date['H'];
00124                 else if (isset($date['h']) && isset($date['a']) ) $params['hours'] = date('H', strtotime($date['h'].":00".$date['a']));
00125                 else if (isset($date['h']) && isset($date['A']) ) $params['hours'] = date('H', strtotime($date['h'].":00".$date['A']));
00126                 else if (isset($date['h']) ) $params['hours'] = $date['h'];
00127                 else $params['hours'] = null;//date('H');
00128                 
00129                 $params['minutes'] = isset( $date['i'] ) ? $date['i'] : null;//date('i');
00130                 $params['seconds'] = isset( $date['s'] ) ? $date['s'] : null;//date('s');
00131                 
00132                 foreach ( array_keys($params) as $param){
00133                         $params[$param] = intval($params[$param]);
00134                 }
00135         
00136                 
00137                 return $params;
00138         
00139         }
00140         
00144         static function isTimeStamp($value){
00145                 if ( is_array($value) ) return false;
00146                 
00147                 return preg_match('/^\d{4,14}$/',$value);
00148         }
00149         
00150         
00155         static function date_to_string($value){
00156                 if ( !isset($value) or !is_array($value) or count($value) == 0 ) return '';
00157                 return  str_pad($value['year'],4,"0",STR_PAD_LEFT).'-'.
00158                                 str_pad($value['month'],2,"0",STR_PAD_LEFT).'-'.
00159                                 str_pad($value['day'],2,"0",STR_PAD_LEFT);
00160         
00161         }
00162         
00166         static function datetime_to_string($value){ 
00167                 if ( !isset($value) or !is_array($value) or count($value) == 0) return '';
00168                 return  str_pad($value['year'],4,"0",STR_PAD_LEFT).'-'.
00169                                 str_pad($value['month'],2,"0",STR_PAD_LEFT).'-'.
00170                                 str_pad($value['day'], 2,"0", STR_PAD_LEFT).' '.
00171                                 str_pad($value['hours'],2,"0",STR_PAD_LEFT).':'.
00172                                 str_pad($value['minutes'],2,"0",STR_PAD_LEFT).':'.
00173                                 str_pad($value['seconds'],2,"0", STR_PAD_LEFT);
00174         }
00175         
00179         static function time_to_string($value){ 
00180                 if ( !isset($value) or !is_array($value) or count($value) == 0 ) return '';
00181                 return  str_pad($value['hours'],2,"0",STR_PAD_LEFT).':'.
00182                                 str_pad($value['minutes'],2,"0", STR_PAD_LEFT).':'.
00183                                 str_pad($value['seconds'],2,"0", STR_PAD_LEFT);
00184         }
00185         
00189         static function timestamp_to_string($value){ 
00190                 if ( !isset($value) or !is_array($value) or count($value) == 0) return '';
00191                 return  str_pad($value['year'],4,"0", STR_PAD_LEFT).
00192                                 str_pad($value['month'],2,"0", STR_PAD_LEFT).
00193                                 str_pad($value['day'],2,"0",STR_PAD_LEFT).
00194                                 str_pad($value['hours'],2,"0", STR_PAD_LEFT).
00195                                 str_pad($value['minutes'],2,"0", STR_PAD_LEFT).
00196                                 str_pad($value['seconds'],2,"0", STR_PAD_LEFT);
00197         }
00198         
00199         static function inRange($date){
00200                 if ( version_compare(PHP_VERSION, '5.1', '<') ){
00201                         return (strtotime($date) == -1);
00202                 } else {
00203                         return (strtotime($date) === false);
00204                 }
00205         }
00206         
00207         
00208 
00209 
00210 }
 All Data Structures Namespaces Files Functions Variables Enumerations