[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/fields/ -> DateTimeField.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  require_once  'include/utils/utils.php';
  11  
  12  class DateTimeField {
  13  
  14      static protected $databaseTimeZone = null;
  15      protected $datetime;
  16      private static $cache = array();
  17  
  18      /**
  19       *
  20       * @param type $value
  21       */
  22  	public function __construct($value) {
  23          if(empty($value)) {
  24              $value = date("Y-m-d H:i:s");
  25          }
  26          $this->date = null;
  27          $this->time = null;
  28          $this->datetime = $value;
  29      }
  30  
  31      /** Function to set date values compatible to database (YY_MM_DD)
  32       * @param $user -- value :: Type Users
  33       * @returns $insert_date -- insert_date :: Type string
  34       */
  35  	function getDBInsertDateValue($user = null) {
  36          global $log;
  37          $log->debug("Entering getDBInsertDateValue(" . $this->datetime . ") method ...");
  38          $value = explode(' ', $this->datetime);
  39          if (count($value) == 2) {
  40              $value[0] = self::convertToUserFormat($value[0]);
  41          }
  42  
  43          $insert_time = '';
  44          if ($value[1] != '') {
  45              $date = self::convertToDBTimeZone($this->datetime, $user);
  46              $insert_date = $date->format('Y-m-d');
  47          } else {
  48              $insert_date = self::convertToDBFormat($value[0]);
  49          }
  50          $log->debug("Exiting getDBInsertDateValue method ...");
  51          return $insert_date;
  52      }
  53  
  54      /**
  55       *
  56       * @param Users $user
  57       * @return String
  58       */
  59  	public function getDBInsertDateTimeValue($user = null) {
  60          return $this->getDBInsertDateValue($user) . ' ' .
  61                  $this->getDBInsertTimeValue($user);
  62      }
  63  
  64  	public function getDisplayDateTimeValue ($user = null) {
  65          return $this->getDisplayDate($user) . ' ' . $this->getDisplayTime($user);
  66      }
  67  
  68      /**
  69       *
  70       * @global Users $current_user
  71       * @param type $date
  72       * @param Users $user
  73       * @return type
  74       */
  75  	public static function convertToDBFormat($date, $user = null) {
  76          global $current_user;
  77          if(empty($user)) {
  78              $user = $current_user;
  79          }
  80  
  81          $format = $current_user->date_format;
  82          if(empty($format)) {
  83              $format = 'dd-mm-yyyy';
  84          }
  85  
  86          return self::__convertToDBFormat($date, $format);
  87      }
  88  
  89      /**
  90       *
  91       * @param type $date
  92       * @param string $format
  93       * @return string
  94       */
  95  	public static function __convertToDBFormat($date, $format) {
  96  
  97          if ($format == '') {
  98              $format = 'dd-mm-yyyy';
  99          }
 100          $dbDate = '';
 101          if ($format == 'dd-mm-yyyy') {
 102              list($d, $m, $y) = explode('-', $date);
 103          } elseif ($format == 'mm-dd-yyyy') {
 104              list($m, $d, $y) = explode('-', $date);
 105          } elseif ($format == 'yyyy-mm-dd') {
 106              list($y, $m, $d) = explode('-', $date);
 107          }
 108  
 109          if (!$y && !$m && !$d) {
 110              $dbDate = '';
 111          } else {
 112              $dbDate = $y . '-' . $m . '-' . $d;
 113          }
 114          return $dbDate;
 115      }
 116  
 117      /**
 118       *
 119       * @param Mixed $date
 120       * @return Array
 121       */
 122  	public static function convertToInternalFormat($date) {
 123          if(!is_array($date)) {
 124              $date = explode(' ', $date);
 125          }
 126          return $date;
 127      }
 128  
 129      /**
 130       *
 131       * @global Users $current_user
 132       * @param type $date
 133       * @param Users $user
 134       * @return type
 135       */
 136  	public static function convertToUserFormat($date, $user = null) {
 137          global $current_user;
 138          if(empty($user)) {
 139              $user = $current_user;
 140          }
 141          $format = $user->date_format;
 142          if(empty($format)) {
 143              $format = 'dd-mm-yyyy';
 144          }
 145          return self::__convertToUserFormat($date, $format);
 146      }
 147  
 148      /**
 149       *
 150       * @param type $date
 151       * @param type $format
 152       * @return type
 153       */
 154  	public static function __convertToUserFormat($date, $format) {
 155          $date = self::convertToInternalFormat($date);
 156          list($y, $m, $d) = explode('-', $date[0]);
 157  
 158          if ($format == 'dd-mm-yyyy') {
 159              $date[0] = $d . '-' . $m . '-' . $y;
 160          } elseif ($format == 'mm-dd-yyyy') {
 161              $date[0] = $m . '-' . $d . '-' . $y;
 162          } elseif ($format == 'yyyy-mm-dd') {
 163              $date[0] = $y . '-' . $m . '-' . $d;
 164          }
 165          if ($date[1] != '') {
 166              $userDate = $date[0] . ' ' . $date[1];
 167          } else {
 168              $userDate = $date[0];
 169          }
 170          return $userDate;
 171      }
 172  
 173      /**
 174       *
 175       * @global Users $current_user
 176       * @param type $value
 177       * @param Users $user
 178       */
 179  	public static function convertToUserTimeZone($value, $user = null ) {
 180          global $current_user, $default_timezone;
 181          if(empty($user)) {
 182              $user = $current_user;
 183          }
 184          $timeZone = $user->time_zone ? $user->time_zone : $default_timezone;
 185          return DateTimeField::convertTimeZone($value, self::getDBTimeZone(), $timeZone);
 186      }
 187  
 188      /**
 189       *
 190       * @global Users $current_user
 191       * @param type $value
 192       * @param Users $user
 193       */
 194  	public static function convertToDBTimeZone( $value, $user = null ) {
 195          global $current_user, $default_timezone;
 196          if(empty($user)) {
 197              $user = $current_user;
 198          }
 199          $timeZone = $user->time_zone ? $user->time_zone : $default_timezone;
 200          $value = self::sanitizeDate($value, $user);
 201          return DateTimeField::convertTimeZone($value, $timeZone, self::getDBTimeZone() );
 202      }
 203  
 204      /**
 205       *
 206       * @param type $time
 207       * @param type $sourceTimeZoneName
 208       * @param type $targetTimeZoneName
 209       * @return DateTime
 210       */
 211  	public static function convertTimeZone($time, $sourceTimeZoneName, $targetTimeZoneName) {
 212          // TODO Caching is causing problem in getting the right date time format in Calendar module.
 213          // Need to figure out the root cause for the problem. Till then, disabling caching.
 214          //if(empty(self::$cache[$time][$targetTimeZoneName])) {
 215              // create datetime object for given time in source timezone
 216              $sourceTimeZone = new DateTimeZone($sourceTimeZoneName);
 217              if($time == '24:00') $time = '00:00';
 218              $myDateTime = new DateTime($time, $sourceTimeZone);
 219  
 220              // convert this to target timezone using the DateTimeZone object
 221              $targetTimeZone = new DateTimeZone($targetTimeZoneName);
 222              $myDateTime->setTimeZone($targetTimeZone);
 223              self::$cache[$time][$targetTimeZoneName] = $myDateTime;
 224          //}
 225          $myDateTime = self::$cache[$time][$targetTimeZoneName];
 226          return $myDateTime;
 227      }
 228  
 229      /** Function to set timee values compatible to database (GMT)
 230       * @param $user -- value :: Type Users
 231       * @returns $insert_date -- insert_date :: Type string
 232       */
 233  	function getDBInsertTimeValue($user = null) {
 234          global $log;
 235          $log->debug("Entering getDBInsertTimeValue(" . $this->datetime . ") method ...");
 236          $date = self::convertToDBTimeZone($this->datetime, $user);
 237          $log->debug("Exiting getDBInsertTimeValue method ...");
 238          return $date->format("H:i:s");
 239      }
 240  
 241      /**
 242       * This function returns the date in user specified format.
 243       * @global type $log
 244       * @global Users $current_user
 245       * @return string
 246       */
 247  	function getDisplayDate( $user = null ) {
 248          global $log;
 249          $log->debug("Entering getDisplayDate(" . $this->datetime . ") method ...");
 250  
 251          $date_value = explode(' ',$this->datetime);
 252          if ($date_value[1] != '') {
 253              $date = self::convertToUserTimeZone($this->datetime, $user);
 254              $date_value = $date->format('Y-m-d');
 255          }
 256  
 257          $display_date = self::convertToUserFormat($date_value, $user);
 258          $log->debug("Exiting getDisplayDate method ...");
 259          return $display_date;
 260      }
 261  
 262  	function getDisplayTime( $user = null ) {
 263          global $log;
 264          $log->debug("Entering getDisplayTime(" . $this->datetime . ") method ...");
 265          $date = self::convertToUserTimeZone($this->datetime, $user);
 266          $time = $date->format("H:i:s");
 267          $log->debug("Exiting getDisplayTime method ...");
 268          return $time;
 269      }
 270  
 271  	static function getDBTimeZone() {
 272          if(empty(self::$databaseTimeZone)) {
 273              $defaultTimeZone = date_default_timezone_get();
 274              if(empty($defaultTimeZone)) {
 275                  $defaultTimeZone = 'UTC';
 276              }
 277              self::$databaseTimeZone = $defaultTimeZone;
 278          }
 279          return self::$databaseTimeZone;
 280      }
 281  
 282  	static function getPHPDateFormat( $user = null) {
 283          global $current_user;
 284          if(empty($user)) {
 285              $user = $current_user;
 286          }
 287          return str_replace(array('yyyy', 'mm','dd'), array('Y', 'm', 'd'), $user->date_format);
 288      }
 289  
 290  	private static function sanitizeDate($value, $user) {
 291          global $current_user;
 292          if(empty($user)) {
 293              $user = $current_user;
 294          }
 295  
 296          if($user->date_format == 'mm-dd-yyyy') {
 297              list($date, $time) = explode(' ', $value);
 298              if(!empty($date)) {
 299                  list($m, $d, $y) = explode('-', $date);
 300                  if(strlen($m) < 3) {
 301                      $time = ' '.$time;
 302                      $value = "$y-$m-$d".rtrim($time);
 303                  }
 304              }
 305          }
 306          return $value;
 307      }
 308  
 309  
 310  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1