| [ PHPXref.com ] | [ Generated: Sun Jul 20 19:05:09 2008 ] | [ onPHP 0.4.6 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 /*************************************************************************** 3 * Copyright (C) 2005 by Konstantin V. Arkhipov, Igor V. Gulyaev * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License as published by * 7 * the Free Software Foundation; either version 2 of the License, or * 8 * (at your option) any later version. * 9 * * 10 ***************************************************************************/ 11 /* $Id: DateRangeList.class.php 1684 2006-06-10 20:58:39Z voxus $ */ 12 13 /** 14 * @ingroup Primitives 15 **/ 16 class DateRangeList extends BasePrimitive implements Stringable 17 { 18 public function import(&$scope) 19 { 20 if ( 21 !isset($scope[$this->name]) 22 || !is_array($scope[$this->name]) 23 || empty($scope[$this->name]) 24 || 25 ( 26 count($scope[$this->name]) == 1 27 && !current($scope[$this->name]) 28 ) 29 ) 30 return null; 31 32 $array = $scope[$this->name]; 33 $list = array(); 34 35 foreach ($array as $string) 36 if (array() !== self::stringToDateRangeList($string)) 37 foreach (self::stringToDateRangeList($string) as $range) 38 $list[] = $range; 39 40 $this->value = $list; 41 42 return ($this->value !== array()); 43 } 44 45 public function toString() 46 { 47 if ($this->value) { 48 $out = array(); 49 50 foreach ($this->value as $range) 51 $out[] = $range->toDateString(); 52 53 return implode(', ', $out); 54 } 55 56 return null; 57 } 58 59 public static function stringToDateRangeList($string) 60 { 61 $list = array(); 62 63 if (!empty($string)) { 64 if (strpos($string, ',') !== false) 65 $dates = explode(',', $string); 66 else 67 $dates = array($string); 68 69 foreach ($dates as $date) { 70 try { 71 $list[] = self::makeRange($date); 72 } catch (WrongArgumentException $e) { 73 // ignore? 74 } 75 } 76 } 77 78 return $list; 79 } 80 81 public static function printRange(DateRange $range, $delimiter = '-') 82 { 83 $rangeString = null; 84 85 if ($range->getStart()) 86 $rangeString .= $range->getStart()->toDate($delimiter); 87 88 $rangeString .= ' - '; 89 90 if ($range->getEnd()) 91 $rangeString .= $range->getEnd()->toDate($delimiter); 92 93 return $rangeString; 94 } 95 96 public static function makeRange($string) 97 { 98 if ( 99 (substr_count($string, ' - ') === 1) 100 || (substr_count($string, '-')) 101 ) { 102 $delimiter = ' - '; 103 104 if (substr_count($string, '-') === 1) 105 $delimiter = '-'; 106 107 list($start, $finish) = explode($delimiter, $string, 2); 108 109 $start = self::toDate(trim($start)); 110 $finish = self::toDate(trim($finish)); 111 112 if ($start || $finish) { 113 114 $range = new DateRange(); 115 116 $range = 117 DateRange::create()-> 118 lazySet($start, $finish); 119 120 return $range; 121 122 } elseif ($string == ' - ') 123 return DateRange::create(); 124 125 } elseif ($single = self::toDate(trim($string))) 126 return 127 DateRange::create()-> 128 setStart($single)-> 129 setEnd($single); 130 131 throw new WrongArgumentException( 132 "unknown string format '{$string}'" 133 ); 134 } 135 136 private static function toDate($date) 137 { 138 if (strpos($date, '.') !== false) { 139 140 $dots = substr_count($date, '.'); 141 142 $year = null; 143 144 if ($dots == 2) { 145 list($day, $month, $year) = explode('.', $date, ($dots + 1)); 146 147 if (strlen($day) > 2) { 148 $tmp = $year; 149 $year = $day; 150 $day = $tmp; 151 } 152 } else 153 list($day, $month) = explode('.', $date, ($dots + 1)); 154 155 if (strlen($day) == 1) 156 $day = "0{$day}"; 157 158 if ($month === null) 159 $month = date('m'); 160 elseif (strlen($month) == 1) 161 $month = "0{$month}"; 162 163 if ($year === null) 164 $year = date('Y'); 165 // we're all die in 2100+ anyway 166 elseif (strlen($year) === 2) 167 $year = "20{$year}"; 168 169 $date = $year.$month.$day; 170 } 171 172 $lenght = strlen($date); 173 174 try { 175 if ($lenght > 4) 176 return new Timestamp(strtotime($date)); 177 elseif ($lenght === 4) { 178 $date = substr($date, 2).'-'.substr($date, 0, 2); 179 180 return new Timestamp(strtotime(date('Y-').$date)); 181 } elseif (($lenght == 2) || ($lenght == 1)) { 182 return new Timestamp(strtotime(date('Y-m-').$date)); 183 } 184 } catch (WrongArgumentException $e) { 185 // seems to be unparseable 186 } 187 188 return null; 189 } 190 } 191 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |