Quick Search:

View

Revision:

Diff

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