hpdl
|
1
|
1
|
<?php
|
|
2
|
/*
|
hpdl
|
153
|
3
|
$Id: datetime.php 386 2006-01-12 11:36:39Z 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
|
386
|
18
|
function getShort($date = '') {
|
|
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) {
|
|
33
|
return strftime($osC_Language->getDateFormatShort(), mktime($hour, $minute, $second, $month, $day, $year));
|
hpdl
|
1
|
34
|
} else {
|
hpdl
|
386
|
35
|
return ereg_replace('2037', $year, strftime($osC_Language->getDateFormatShort(), 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
|
|
|
60
|
function isLeapYear($year = '') {
|
|
61
|
if (empty($year)) {
|
|
62
|
$year = $this->year;
|
|
63
|
}
|
|
64
|
|
|
65
|
if ($year % 100 == 0) {
|
|
66
|
if ($year % 400 == 0) {
|
|
67
|
return true;
|
|
68
|
}
|
|
69
|
} else {
|
|
70
|
if (($year % 4) == 0) {
|
|
71
|
return true;
|
|
72
|
}
|
|
73
|
}
|
|
74
|
|
|
75
|
return false;
|
|
76
|
}
|
|
77
|
|
|
78
|
function validate($date_to_check, $format_string, &$date_array) {
|
|
79
|
$separator_idx = -1;
|
|
80
|
|
|
81
|
$separators = array('-', ' ', '/', '.');
|
|
82
|
$month_abbr = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
|
|
83
|
$no_of_days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
|
|
84
|
|
|
85
|
$format_string = strtolower($format_string);
|
|
86
|
|
|
87
|
if (strlen($date_to_check) != strlen($format_string)) {
|
|
88
|
return false;
|
|
89
|
}
|
|
90
|
|
|
91
|
$size = sizeof($separators);
|
|
92
|
for ($i=0; $i<$size; $i++) {
|
|
93
|
$pos_separator = strpos($date_to_check, $separators[$i]);
|
|
94
|
if ($pos_separator != false) {
|
|
95
|
$date_separator_idx = $i;
|
|
96
|
break;
|
|
97
|
}
|
|
98
|
}
|
|
99
|
|
|
100
|
for ($i=0; $i<$size; $i++) {
|
|
101
|
$pos_separator = strpos($format_string, $separators[$i]);
|
|
102
|
if ($pos_separator != false) {
|
|
103
|
$format_separator_idx = $i;
|
|
104
|
break;
|
|
105
|
}
|
|
106
|
}
|
|
107
|
|
|
108
|
if ($date_separator_idx != $format_separator_idx) {
|
|
109
|
return false;
|
|
110
|
}
|
|
111
|
|
|
112
|
if ($date_separator_idx != -1) {
|
|
113
|
$format_string_array = explode( $separators[$date_separator_idx], $format_string );
|
|
114
|
if (sizeof($format_string_array) != 3) {
|
|
115
|
return false;
|
|
116
|
}
|
|
117
|
|
|
118
|
$date_to_check_array = explode( $separators[$date_separator_idx], $date_to_check );
|
|
119
|
if (sizeof($date_to_check_array) != 3) {
|
|
120
|
return false;
|
|
121
|
}
|
|
122
|
|
|
123
|
$size = sizeof($format_string_array);
|
|
124
|
for ($i=0; $i<$size; $i++) {
|
|
125
|
if ($format_string_array[$i] == 'mm' || $format_string_array[$i] == 'mmm') $month = $date_to_check_array[$i];
|
|
126
|
if ($format_string_array[$i] == 'dd') $day = $date_to_check_array[$i];
|
|
127
|
if ( ($format_string_array[$i] == 'yyyy') || ($format_string_array[$i] == 'aaaa') ) $year = $date_to_check_array[$i];
|
|
128
|
}
|
|
129
|
} else {
|
|
130
|
if (strlen($format_string) == 8 || strlen($format_string) == 9) {
|
|
131
|
$pos_month = strpos($format_string, 'mmm');
|
|
132
|
if ($pos_month != false) {
|
|
133
|
$month = substr( $date_to_check, $pos_month, 3 );
|
|
134
|
$size = sizeof($month_abbr);
|
|
135
|
for ($i=0; $i<$size; $i++) {
|
|
136
|
if ($month == $month_abbr[$i]) {
|
|
137
|
$month = $i;
|
|
138
|
break;
|
|
139
|
}
|
|
140
|
}
|
|
141
|
} else {
|
|
142
|
$month = substr($date_to_check, strpos($format_string, 'mm'), 2);
|
|
143
|
}
|
|
144
|
} else {
|
|
145
|
return false;
|
|
146
|
}
|
|
147
|
|
|
148
|
$day = substr($date_to_check, strpos($format_string, 'dd'), 2);
|
|
149
|
$year = substr($date_to_check, strpos($format_string, 'yyyy'), 4);
|
|
150
|
}
|
|
151
|
|
|
152
|
if (strlen($year) != 4) {
|
|
153
|
return false;
|
|
154
|
}
|
|
155
|
|
|
156
|
if (!settype($year, 'integer') || !settype($month, 'integer') || !settype($day, 'integer')) {
|
|
157
|
return false;
|
|
158
|
}
|
|
159
|
|
|
160
|
if ($month > 12 || $month < 1) {
|
|
161
|
return false;
|
|
162
|
}
|
|
163
|
|
|
164
|
if ($day < 1) {
|
|
165
|
return false;
|
|
166
|
}
|
|
167
|
|
|
168
|
if ($this->isLeapYear($year)) {
|
|
169
|
$no_of_days[1] = 29;
|
|
170
|
}
|
|
171
|
|
|
172
|
if ($day > $no_of_days[$month - 1]) {
|
|
173
|
return false;
|
|
174
|
}
|
|
175
|
|
|
176
|
$date_array = array($year, $month, $day);
|
|
177
|
|
|
178
|
return true;
|
|
179
|
}
|
|
180
|
}
|
|
181
|
?>
|