Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
date.php
Go to the documentation of this file.
1 <?php
2 /*-------------------------------------------------------------------------------
3  * Xataface Web Application Framework
4  * Copyright (C) 2005-2008 Web Lite Solutions Corp (shannah@sfu.ca)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *-------------------------------------------------------------------------------
20  */
30 
35  static function qf2UnixTimestamp($value){
36  $date = Dataface_converters_date::qf2Table($value);
37  $timestamp = strtotime( Dataface_converters_date::datetime_to_string($date) );
38  return $timestamp;
39  }
40 
45  static function qf2Table($value){
46  $out = array();
47  foreach ($value as $key=>$val){
48  if ( is_array($val) ) $out[$key] = $val[0];
49  else $out[$key] = $val;
50  }
52  }
53 
54 
59  static function parseDate($value){
60  if ( !isset($value) || !$value ) return null;
61  if ( $value == '0000-00-00' || $value == '0000-00-00 00:00:00' ) return null;
62  if ( is_array($value) and (isset( $value['year']) or isset($value['hours'])) ) return $value;
63  // if it is already in the correct format, we don't need to parse it.
64 
66  $date = array();
67  if ( strlen($value)>=4) $date['Y'] = substr($value,0,4);
68  if ( strlen($value)>=6) $date['m'] = substr($value,4,2);
69  if ( strlen($value)>=8) $date['d'] = substr($value,6,2);
70  if ( strlen($value)>=10) $date['H'] = substr($value,8,2);
71  if ( strlen($value)>=12) $date['i'] = substr($value,10,2);
72  if ( strlen($value)>=14) $date['s'] = substr($value,12,2);
73  }
74  else if ( !is_array($value) ){
75  if ( function_exists('date_parse') ){
76  $out = date_parse($value);
77  $out['hours'] = $out['hour'];
78  unset($out['hour']);
79  $out['minutes'] = $out['minute'];
80  unset($out['minute']);
81  $out['seconds'] = $out['second'];
82  unset($out['second']);
83  return $out;
84  } else if ( Dataface_converters_date::inRange($value) ){
85  // strtotime cannot seem to calculate the time properly on this
86  // so we will manually parse it;
87  if ( preg_match('/^(\d{4})(-(\d{2}))?(-(\d{2}))?( (\d{2}):(\d{2})(:(\d{2}))?)?$/', $value, $matches)){
88  $date = array();
89  $date['year'] = $matches[1];
90  $date['month'] = @$matches[3];
91  $date['day'] = @$matches[5];
92  $date['hours'] = @$matches[7];
93  $date['minutes'] = @$matches[8];
94  $date['seconds'] = @$matches[10];
95  return $date;
96  }
97 
98  }
99  $isNull = true;
100  $units = explode(' ','Y m M F d h a A i s');
101  $date = array();
102  foreach ($units as $unit){
103  if ( $value ){
104  $date[$unit] = date($unit, strtotime($value));
105  $isNull = false;
106  } else {
107  $date[$unit] = null; //date($unit);
108  }
109  }
110  if ( $isNull ) return null;
111 
112 
113  } else {
114  $date = $value;
115  }
116  $params = array();
117  $params['year'] = isset($date['Y']) ? $date['Y'] : date('Y');
118  $params['month'] = isset($date['m']) ? $date['m'] : (
119  isset($date['M']) ? $date['M'] : (
120  isset($date['F']) ? $date['F'] : null//date('m')
121  ));
122  $params['day'] = isset($date['d']) ? $date['d'] : null;//date('d');
123  if ( isset($date['H'] ) ) $params['hours'] = $date['H'];
124  else if (isset($date['h']) && isset($date['a']) ) $params['hours'] = date('H', strtotime($date['h'].":00".$date['a']));
125  else if (isset($date['h']) && isset($date['A']) ) $params['hours'] = date('H', strtotime($date['h'].":00".$date['A']));
126  else if (isset($date['h']) ) $params['hours'] = $date['h'];
127  else $params['hours'] = null;//date('H');
128 
129  $params['minutes'] = isset( $date['i'] ) ? $date['i'] : null;//date('i');
130  $params['seconds'] = isset( $date['s'] ) ? $date['s'] : null;//date('s');
131 
132  foreach ( array_keys($params) as $param){
133  $params[$param] = intval($params[$param]);
134  }
135 
136 
137  return $params;
138 
139  }
140 
144  static function isTimeStamp($value){
145  if ( is_array($value) ) return false;
146 
147  return preg_match('/^\d{4,14}$/',$value);
148  }
149 
150 
155  static function date_to_string($value){
156  if ( !isset($value) or !is_array($value) or count($value) == 0 ) return '';
157  return str_pad($value['year'],4,"0",STR_PAD_LEFT).'-'.
158  str_pad($value['month'],2,"0",STR_PAD_LEFT).'-'.
159  str_pad($value['day'],2,"0",STR_PAD_LEFT);
160 
161  }
162 
166  static function datetime_to_string($value){
167  if ( !isset($value) or !is_array($value) or count($value) == 0) return '';
168  return str_pad($value['year'],4,"0",STR_PAD_LEFT).'-'.
169  str_pad($value['month'],2,"0",STR_PAD_LEFT).'-'.
170  str_pad($value['day'], 2,"0", STR_PAD_LEFT).' '.
171  str_pad($value['hours'],2,"0",STR_PAD_LEFT).':'.
172  str_pad($value['minutes'],2,"0",STR_PAD_LEFT).':'.
173  str_pad($value['seconds'],2,"0", STR_PAD_LEFT);
174  }
175 
179  static function time_to_string($value){
180  if ( !isset($value) or !is_array($value) or count($value) == 0 ) return '';
181  return str_pad($value['hours'],2,"0",STR_PAD_LEFT).':'.
182  str_pad($value['minutes'],2,"0", STR_PAD_LEFT).':'.
183  str_pad($value['seconds'],2,"0", STR_PAD_LEFT);
184  }
185 
189  static function timestamp_to_string($value){
190  return self::datetime_to_string($value);
191  /*
192  // We removed this because timestamps should be displayed like normal dates
193  // http://bugs.weblite.ca/view.php?id=1038
194  if ( !isset($value) or !is_array($value) or count($value) == 0) return '';
195  return str_pad($value['year'],4,"0", STR_PAD_LEFT).
196  str_pad($value['month'],2,"0", STR_PAD_LEFT).
197  str_pad($value['day'],2,"0",STR_PAD_LEFT).
198  str_pad($value['hours'],2,"0", STR_PAD_LEFT).
199  str_pad($value['minutes'],2,"0", STR_PAD_LEFT).
200  str_pad($value['seconds'],2,"0", STR_PAD_LEFT);
201  */
202  }
203 
204  static function inRange($date){
205  if ( version_compare(PHP_VERSION, '5.1', '<') ){
206  return (strtotime($date) == -1);
207  } else {
208  return (strtotime($date) === false);
209  }
210  }
211 
212 
213 
214 
215 }