Quick Search:

View

Revision:

Diff

Diff from 43 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/datetime.php

Annotated File View

hpdl
1
1 <?php
2 /*
3   $Id: datetime.php 5 2005-01-31 01:40:15Z hpdl $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2004 osCommerce
9
10   Released under the GNU General Public License
11 */
12
13   class osC_DateTime {
14     var $date = '',
15         $month = '',
16         $year = '',
17         $hour = '',
18         $minute = '',
19         $second = '',
20         $long_date_format = '',
21         $short_date_format = '',
22         $long_date = '',
23         $short_date = '';
24
25     function osC_DateTime($raw_date = '', $long_date_format = '', $short_date_format = '') {
26       if (tep_not_null($long_date_format)) {
27         $this->long_date_format = $long_date_format;
28       } else {
29         $this->long_date_format = DATE_FORMAT_LONG;
30       }
31
32       if (tep_not_null($short_date_format)) {
33         $this->short_date_format = $short_date_format;
34       } else {
35         $this->short_date_format = DATE_FORMAT;
36       }
37
38       if (tep_not_null($raw_date)) {
39         $this->format($raw_date);
40       } else {
41         $this->setCurrentDate();
42       }
43     }
44
45     function setCurrentDate() {
46       $this->date = date('d');
47       $this->month = date('m');
48       $this->year = date('Y');
49
50       $this->hour = date('H');
51       $this->minute = date('i');
52       $this->second = date('s');
53     }
54
55     function format($raw_date) {
56       if (empty($raw_date) || ($raw_date == '0000-00-00 00:00:00')) {
57         return false;
58       }
59
60       $this->year = (int)substr($raw_date, 0, 4);
61       $this->month = (int)substr($raw_date, 5, 2);
62       $this->day = (int)substr($raw_date, 8, 2);
63       $this->hour = (int)substr($raw_date, 11, 2);
64       $this->minute = (int)substr($raw_date, 14, 2);
65       $this->second = (int)substr($raw_date, 17, 2);
66
67       $this->long_date = strftime($this->long_date_format, mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year));
68
69       if (@date('Y', mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year)) == $this->year) {
70         $this->short_date = date($this->short_date_format, mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year));
71       } else {
72         $this->short_date = ereg_replace('2037$', $this->year, date($this->short_date_format, mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, 2037)));
73       }
74     }
75
76     function isLeapYear($year = '') {
77       if (empty($year)) {
78         $year = $this->year;
79       }
80
81       if ($year % 100 == 0) {
82         if ($year % 400 == 0) {
83           return true;
84         }
85       } else {
86         if (($year % 4) == 0) {
87           return true;
88         }
89       }
90
91       return false;
92     }
93
94     function validate($date_to_check, $format_string, &$date_array) {
95       $separator_idx = -1;
96
97       $separators = array('-', ' ', '/', '.');
98       $month_abbr = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
99       $no_of_days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
100
101       $format_string = strtolower($format_string);
102
103       if (strlen($date_to_check) != strlen($format_string)) {
104         return false;
105       }
106
107       $size = sizeof($separators);
108       for ($i=0; $i<$size; $i++) {
109         $pos_separator = strpos($date_to_check, $separators[$i]);
110         if ($pos_separator != false) {
111           $date_separator_idx = $i;
112           break;
113         }
114       }
115
116       for ($i=0; $i<$size; $i++) {
117         $pos_separator = strpos($format_string, $separators[$i]);
118         if ($pos_separator != false) {
119           $format_separator_idx = $i;
120           break;
121         }
122       }
123
124       if ($date_separator_idx != $format_separator_idx) {
125         return false;
126       }
127
128       if ($date_separator_idx != -1) {
129         $format_string_array = explode( $separators[$date_separator_idx], $format_string );
130         if (sizeof($format_string_array) != 3) {
131           return false;
132         }
133
134         $date_to_check_array = explode( $separators[$date_separator_idx], $date_to_check );
135         if (sizeof($date_to_check_array) != 3) {
136           return false;
137         }
138
139         $size = sizeof($format_string_array);
140         for ($i=0; $i<$size; $i++) {
141           if ($format_string_array[$i] == 'mm' || $format_string_array[$i] == 'mmm') $month = $date_to_check_array[$i];
142           if ($format_string_array[$i] == 'dd') $day = $date_to_check_array[$i];
143           if ( ($format_string_array[$i] == 'yyyy') || ($format_string_array[$i] == 'aaaa') ) $year = $date_to_check_array[$i];
144         }
145       } else {
146         if (strlen($format_string) == 8 || strlen($format_string) == 9) {
147           $pos_month = strpos($format_string, 'mmm');
148           if ($pos_month != false) {
149             $month = substr( $date_to_check, $pos_month, 3 );
150             $size = sizeof($month_abbr);
151             for ($i=0; $i<$size; $i++) {
152               if ($month == $month_abbr[$i]) {
153                 $month = $i;
154                 break;
155               }
156             }
157           } else {
158             $month = substr($date_to_check, strpos($format_string, 'mm'), 2);
159           }
160         } else {
161           return false;
162         }
163
164         $day = substr($date_to_check, strpos($format_string, 'dd'), 2);
165         $year = substr($date_to_check, strpos($format_string, 'yyyy'), 4);
166       }
167
168       if (strlen($year) != 4) {
169         return false;
170       }
171
172       if (!settype($year, 'integer') || !settype($month, 'integer') || !settype($day, 'integer')) {
173         return false;
174       }
175
176       if ($month > 12 || $month < 1) {
177         return false;
178       }
179
180       if ($day < 1) {
181         return false;
182       }
183
184       if ($this->isLeapYear($year)) {
185         $no_of_days[1] = 29;
186       }
187
188       if ($day > $no_of_days[$month - 1]) {
189         return false;
190       }
191
192       $date_array = array($year, $month, $day);
193
194       return true;
195     }
196   }
197 ?>