Quick Search:

View

Revision:

Diff

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