Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*+**********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
************************************************************************************/
require_once 'include/utils/utils.php';
class DateTimeField {
static protected $databaseTimeZone = null;
protected $datetime;
private static $cache = array();
/**
*
* @param type $value
*/
public function __construct($value) {
if(empty($value)) {
$value = date("Y-m-d H:i:s");
}
$this->date = null;
$this->time = null;
$this->datetime = $value;
}
/** Function to set date values compatible to database (YY_MM_DD)
* @param $user -- value :: Type Users
* @returns $insert_date -- insert_date :: Type string
*/
function getDBInsertDateValue($user = null) {
global $log;
$log->debug("Entering getDBInsertDateValue(" . $this->datetime . ") method ...");
$value = explode(' ', $this->datetime);
if (count($value) == 2) {
$value[0] = self::convertToUserFormat($value[0]);
}
$insert_time = '';
if ($value[1] != '') {
$date = self::convertToDBTimeZone($this->datetime, $user);
$insert_date = $date->format('Y-m-d');
} else {
$insert_date = self::convertToDBFormat($value[0]);
}
$log->debug("Exiting getDBInsertDateValue method ...");
return $insert_date;
}
/**
*
* @param Users $user
* @return String
*/
public function getDBInsertDateTimeValue($user = null) {
return $this->getDBInsertDateValue($user) . ' ' .
$this->getDBInsertTimeValue($user);
}
public function getDisplayDateTimeValue ($user = null) {
return $this->getDisplayDate($user) . ' ' . $this->getDisplayTime($user);
}
public function getFullcalenderDateTimevalue ($user = null) {
return $this->getDisplayDate($user) . ' ' . $this->getFullcalenderTime($user);
}
/**
*
* @param string $date
* @param Users $user
*
* @return string
* @global Users $current_user
*/
public static function convertToDBFormat($date, $user = null)
{
global $current_user;
if(empty($user)) {
$user = $current_user;
}
$format = $current_user->date_format;
if (empty($format)) {
if (false === strpos($date, '-')) {
if(false === strpos($date, '.')){
$format = 'dd/mm/yyyy';
} else {
$format = 'dd.mm.yyyy';
}
} else {
$format = 'dd-mm-yyyy';
}
}
return self::__convertToDBFormat($date, $format);
}
/**
*
* @param string $date
* @param string $format
*
* @return string
*/
public static function __convertToDBFormat($date, $format)
{
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
$dbDate = '';
if (empty($format)) {
if (false === strpos($date, '-')) {
if(false === strpos($date, '.')){
$format = 'dd/mm/yyyy';
} else {
$format = 'dd.mm.yyyy';
}
} else {
$format = 'dd-mm-yyyy';
}
}
switch ($format) {
case 'dd.mm.yyyy':
list($d, $m, $y) = explode('.', $date);
break;
case 'dd/mm/yyyy':
list($d, $m, $y) = explode('/', $date);
break;
case 'dd-mm-yyyy':
list($d, $m, $y) = explode('-', $date);
break;
case 'mm-dd-yyyy':
list($m, $d, $y) = explode('-', $date);
break;
case 'yyyy-mm-dd':
list($y, $m, $d) = explode('-', $date);
break;
}
if (!empty($y) && !empty($m) && !empty($d)) {
$dbDate = $y . '-' . $m . '-' . $d;
}
return $dbDate;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
*
* @param Mixed $date
* @return Array
*/
public static function convertToInternalFormat($date) {
if(!is_array($date)) {
$date = explode(' ', $date);
}
return $date;
}
/**
*
* @global Users $current_user
* @param type $date
* @param Users $user
* @return type
*/
public static function convertToUserFormat($date, $user = null) {
global $current_user;
if(empty($user)) {
$user = $current_user;
}
$format = $user->date_format;
if(empty($format)) {
$format = 'dd-mm-yyyy';
}
return self::__convertToUserFormat($date, $format);
}
/**
*
* @param type $date
* @param type $format
*
* @return string
*/
public static function __convertToUserFormat($date, $format)
{
$date = self::convertToInternalFormat($date);
list($y, $m, $d) = explode('-', $date[0]);
switch ($format) {
case 'dd.mm.yyyy':
$date[0] = $d . '.' . $m . '.' . $y;
break;
case 'mm.dd.yyyy':
$date[0] = $m . '.' . $d . '.' . $y;
break;
case 'yyyy.mm.dd':
$date[0] = $y . '.' . $m . '.' . $d;
break;
case 'dd/mm/yyyy':
$date[0] = $d . '/' . $m . '/' . $y;
break;
case 'mm/dd/yyyy':
$date[0] = $m . '/' . $d . '/' . $y;
break;
case 'yyyy/mm/dd':
$date[0] = $y . '/' . $m . '/' . $d;
break;
case 'dd-mm-yyyy':
$date[0] = $d . '-' . $m . '-' . $y;
break;
case 'mm-dd-yyyy':
$date[0] = $m . '-' . $d . '-' . $y;
break;
case 'yyyy-mm-dd':
$date[0] = $y . '-' . $m . '-' . $d;
break;
}
if ($date[1] != '') {
$userDate = $date[0] . ' ' . $date[1];
} else {
$userDate = $date[0];
}
return $userDate;
}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
*
* @global Users $current_user
* @param type $value
* @param Users $user
*/
public static function convertToUserTimeZone($value, $user = null ) {
global $current_user, $default_timezone;
if(empty($user)) {
$user = $current_user;
}
$timeZone = $user->time_zone ? $user->time_zone : $default_timezone;
return DateTimeField::convertTimeZone($value, self::getDBTimeZone(), $timeZone);
}
/**
*
* @global Users $current_user
* @param type $value
* @param Users $user
*/
public static function convertToDBTimeZone( $value, $user = null ) {
global $current_user, $default_timezone;
if(empty($user)) {
$user = $current_user;
}
$timeZone = $user->time_zone ? $user->time_zone : $default_timezone;
$value = self::sanitizeDate($value, $user);
return DateTimeField::convertTimeZone($value, $timeZone, self::getDBTimeZone() );
}
/**
*
* @param type $time
* @param type $sourceTimeZoneName
* @param type $targetTimeZoneName
* @return DateTime
*/
public static function convertTimeZone($time, $sourceTimeZoneName, $targetTimeZoneName) {
// TODO Caching is causing problem in getting the right date time format in Calendar module.
// Need to figure out the root cause for the problem. Till then, disabling caching.
//if(empty(self::$cache[$time][$targetTimeZoneName])) {
// create datetime object for given time in source timezone
$sourceTimeZone = new DateTimeZone($sourceTimeZoneName);
if($time == '24:00') $time = '00:00';
$myDateTime = new DateTime($time, $sourceTimeZone);
// convert this to target timezone using the DateTimeZone object
$targetTimeZone = new DateTimeZone($targetTimeZoneName);
$myDateTime->setTimeZone($targetTimeZone);
self::$cache[$time][$targetTimeZoneName] = $myDateTime;
//}
$myDateTime = self::$cache[$time][$targetTimeZoneName];
return $myDateTime;
}
/** Function to set timee values compatible to database (GMT)
* @param $user -- value :: Type Users
* @returns $insert_date -- insert_date :: Type string
*/
function getDBInsertTimeValue($user = null) {
global $log;
$log->debug("Entering getDBInsertTimeValue(" . $this->datetime . ") method ...");
$date = self::convertToDBTimeZone($this->datetime, $user);
$log->debug("Exiting getDBInsertTimeValue method ...");
return $date->format("H:i:s");
}
/**
* This function returns the date in user specified format.
* @global type $log
* @global Users $current_user
* @return string
*/
function getDisplayDate( $user = null ) {
global $log;
$log->debug("Entering getDisplayDate(" . $this->datetime . ") method ...");
$date_value = explode(' ',$this->datetime);
if ($date_value[1] != '') {
$date = self::convertToUserTimeZone($this->datetime, $user);
$date_value = $date->format('Y-m-d');
}
$display_date = self::convertToUserFormat($date_value, $user);
$log->debug("Exiting getDisplayDate method ...");
return $display_date;
}
function getDisplayTime( $user = null ) {
global $log;
$log->debug("Entering getDisplayTime(" . $this->datetime . ") method ...");
$date = self::convertToUserTimeZone($this->datetime, $user);
$time = $date->format("H:i:s");
$log->debug("Exiting getDisplayTime method ...");
return $time;
}
function getFullcalenderTime( $user = null ) {
global $log;
$log->debug("Entering getDisplayTime(" . $this->datetime . ") method ...");
$date = self::convertToUserTimeZone($this->datetime, $user);
$time = $date->format("H:i:s");
$log->debug("Exiting getDisplayTime method ...");
return $time;
}
static function getDBTimeZone() {
if(empty(self::$databaseTimeZone)) {
$defaultTimeZone = date_default_timezone_get();
if(empty($defaultTimeZone)) {
$defaultTimeZone = 'UTC';
}
self::$databaseTimeZone = $defaultTimeZone;
}
return self::$databaseTimeZone;
}
static function getPHPDateFormat( $user = null) {
global $current_user;
if(empty($user)) {
$user = $current_user;
}
return str_replace(array('yyyy', 'mm','dd'), array('Y', 'm', 'd'), $user->date_format);
}
private static function sanitizeDate($value, $user) {
global $current_user;
if(empty($user)) {
$user = $current_user;
}
$y = false;
$m = false;
$d = false;
$time = false;
if($user->date_format) {
list($date, $time) = explode(' ', $value);
if(!empty($date)) {
switch ($user->date_format) {
case 'mm.dd.yyyy': list($m, $d, $y) = explode('.', $date); break;
case 'dd.mm.yyyy': list($d, $m, $y) = explode('.', $date); break;
case 'dd/mm/yyyy': list($d, $m, $y) = explode('/', $date); break;
case 'mm/dd/yyyy': list($d, $m, $y) = explode('/', $date); break;
case 'mm-dd-yyyy': list($m, $d, $y) = explode('-', $date); break;
case 'dd-mm-yyyy': list($d, $m, $y) = explode('-', $date); break;
if ($y) {
$value = "$y-$m-$d ".rtrim($time);
}