Quick Search:

View

Revision:

Diff

Diff from 758 to:

Annotations

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

Annotated File View

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