/modules/Calendar/Date.php

https://bitbucket.org/jhunsinfotech/blue-blues · PHP · 537 lines · 357 code · 39 blank · 141 comment · 77 complexity · a49e776126675b6913f3ae149656adf8 MD5 · raw file

  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. ********************************************************************************/
  11. class vt_DateTime
  12. {
  13. var $second = 0;
  14. var $minute = 0;
  15. var $hour = 0;
  16. var $z_hour = '00';
  17. var $day;
  18. var $z_day;
  19. var $week;
  20. var $month;
  21. var $z_month;
  22. var $year;
  23. var $dayofweek;
  24. var $dayofyear;
  25. var $daysinmonth;
  26. var $daysinyear;
  27. var $dayofweek_inshort;
  28. var $dayofweek_inlong;
  29. var $month_inshort;
  30. var $month_inlong;
  31. var $ts;
  32. var $offset;
  33. var $format;
  34. var $tz;
  35. var $ts_def;
  36. /**
  37. * Constructor for vt_DateTime class
  38. * @param array $timearr - collection of string
  39. * @param string $check - check string
  40. */
  41. function vt_DateTime(&$timearr,$check)
  42. {
  43. if (! isset( $timearr) || count($timearr) == 0 )
  44. {
  45. $this->setDateTime(null);
  46. }
  47. else if ( isset( $timearr['ts']))
  48. {
  49. $this->setDateTime($time['ts']);
  50. }
  51. else
  52. {
  53. if(isset($timearr['hour']) && $timearr['hour'] !== '')
  54. {
  55. $this->hour = $timearr['hour'];
  56. }
  57. if(isset($timearr['min']) && $timearr['min'] !== '')
  58. {
  59. $this->minute = $timearr['min'];
  60. }
  61. if(isset($timearr['sec']) && $timearr['sec'] !== '')
  62. {
  63. $this->second = $timearr['sec'];
  64. }
  65. if(isset($timearr['day']) && $timearr['day'] !== '')
  66. {
  67. $this->day = $timearr['day'];
  68. }
  69. if(isset($timearr['week']) && $timearr['week'] !== '')
  70. {
  71. $this->week = $timearr['week'];
  72. }
  73. if(isset($timearr['month']) && $timearr['month'] !== '')
  74. {
  75. $this->month = $timearr['month'];
  76. }
  77. if(isset($timearr['year']) && $timearr['year'] >= 1970)
  78. {
  79. $this->year = $timearr['year'];
  80. }
  81. else
  82. {
  83. return null;
  84. }
  85. }
  86. if ($check)
  87. {
  88. $this->getDateTime();
  89. }
  90. }
  91. /**
  92. * function to get date and time using index
  93. * @param integer $index - number between 0 to 23
  94. * @param string $day - date
  95. * @param string $month - month
  96. * @param string $year - year
  97. * return vt_DateTime obj $datetimevalue
  98. */
  99. function getTodayDatetimebyIndex($index,$day='', $month='', $year=''){
  100. if($day === '')
  101. $day = $this->day;
  102. if($month === '')
  103. $month = $this->month;
  104. if($year === '')
  105. $year = $this->year;
  106. $day_array = array();
  107. if($index < 0 || $index > 23){
  108. die("hour is invalid");
  109. }
  110. $day_array['hour'] = $index;
  111. $day_array['min'] = 0;
  112. $day_array['day'] = $day;
  113. $day_array['month'] = $month;
  114. $day_array['year'] = $year;
  115. $datetimevalue = new vt_DateTime($day_array,true);
  116. return $datetimevalue;
  117. }
  118. /**
  119. * function to get days in week using index
  120. * @param integer $index - number between 1 to 7
  121. * return vt_DateTime obj $datetimevalue
  122. */
  123. function getThisweekDaysbyIndex($index){
  124. $week_array = array();
  125. if($index < 1 || $index > 7){
  126. die("day is invalid");
  127. }
  128. $week_array['day'] = $this->day + ($index - $this->dayofweek);
  129. $week_array['month'] = $this->month;
  130. $week_array['year'] = $this->year;
  131. $datetimevalue = new vt_DateTime($week_array,true);
  132. return $datetimevalue;
  133. }
  134. /**
  135. * function to get days in month using index
  136. *
  137. * This function will be deprecated.
  138. * The newer version is getThisMonthsDayByIndex() and should be used wherever possible
  139. *
  140. * @param integer $index - number between 0 to 42
  141. * @param string $day - date
  142. * @param string $month - month
  143. * @param string $year - year
  144. * return vt_DateTime obj $datetimevalue
  145. */
  146. function getThismonthDaysbyIndex($index,$day='', $month='', $year='')
  147. {
  148. if($day == '')
  149. $day = $index+1;
  150. if($month == '')
  151. $month = $this->month;
  152. if($year == '')
  153. $year = $this->year;
  154. $month_array = array();
  155. $month_array['day'] = $day;
  156. $month_array['month'] = $month;
  157. $month_array['year'] = $year;
  158. $datetimevalue = new vt_DateTime($month_array,true);
  159. return $datetimevalue;
  160. }
  161. /**
  162. * function to get months in year using index
  163. * @param integer $index - number between 0 to 11
  164. * return vt_DateTime obj $datetimevalue
  165. */
  166. function getThisyearMonthsbyIndex($index)
  167. {
  168. $year_array = array();
  169. $year_array['day'] = 1;
  170. if($index < 0 || $index > 11)
  171. {
  172. die("month is invalid");
  173. }
  174. $year_array['month'] = $index+1;
  175. $year_array['year'] = $this->year;
  176. $datetimevalue = new vt_DateTime($year_array,true);
  177. return $datetimevalue;
  178. }
  179. /**
  180. * function to get hour end time
  181. * return vt_DateTime obj $datetimevalue
  182. */
  183. function getHourendtime()
  184. {
  185. $date_array = array();
  186. $date_array['hour'] = $this->hour;
  187. $date_array['min'] = 59;
  188. $date_array['day'] = $this->day;
  189. $date_array['sec'] = 59;
  190. $date_array['month'] = $this->month;
  191. $date_array['year'] = $this->year;
  192. $datetimevalue = new vt_DateTime($date_array,true);
  193. return $datetimevalue;
  194. }
  195. /**
  196. * function to get day end time
  197. * return vt_DateTime obj $datetimevalue
  198. */
  199. function getDayendtime()
  200. {
  201. $date_array = array();
  202. $date_array['hour'] = 23;
  203. $date_array['min'] = 59;
  204. $date_array['sec'] = 59;
  205. $date_array['day'] = $this->day;
  206. $date_array['month'] = $this->month;
  207. $date_array['year'] = $this->year;
  208. $datetimevalue = new vt_DateTime($date_array,true);
  209. return $datetimevalue;
  210. }
  211. /**
  212. * function to get month end time
  213. * return vt_DateTime obj $datetimevalue
  214. */
  215. function getMonthendtime()
  216. {
  217. $date_array = array();
  218. $date_array['hour'] = 23;
  219. $date_array['min'] = 59;
  220. $date_array['sec'] = 59;
  221. $date_array['day'] = $this->daysinmonth;
  222. $date_array['month'] = $this->month;
  223. $date_array['year'] = $this->year;
  224. $datetimevalue = new vt_DateTime($date_array,true);
  225. return $datetimevalue;
  226. }
  227. /**
  228. * function to get day of week
  229. * return string $this->day - day (eg: Monday)
  230. */
  231. function get_Date()
  232. {
  233. return $this->day;
  234. }
  235. /**
  236. * function to get month name in short
  237. * return string $this->month_inshort - month name (eg: Jan)
  238. */
  239. function getmonthName_inshort(){
  240. return $this->month_inshort;
  241. }
  242. /**
  243. * function to get month
  244. * return string $this->month - month name
  245. */
  246. function getMonth(){
  247. return $this->month;
  248. }
  249. /**
  250. * function to get year
  251. */
  252. function getYear() {
  253. return $this->year;
  254. }
  255. /**
  256. * function to get the number of days in a month
  257. */
  258. function getDaysInMonth() {
  259. return $this->daysinmonth;
  260. }
  261. /**
  262. * function to get month name
  263. * return string $this->month_inlong - month name
  264. */
  265. function getmonthName(){
  266. return $this->month_inlong;
  267. }
  268. /**
  269. * function to get day of week
  270. * return string $this->dayofweek_inlong - day of week
  271. */
  272. function getdayofWeek(){
  273. return $this->dayofweek_inlong;
  274. }
  275. /**
  276. * function to get day of week in short
  277. * return string $this->dayofweek_inshort - day of week (eg: Mon)
  278. */
  279. function getdayofWeek_inshort(){
  280. return $this->dayofweek_inshort;
  281. }
  282. /**
  283. * function to set values for vt_DateTime object
  284. * @param integer $ts - Time stamp
  285. */
  286. function setDateTime($ts){
  287. global $mod_strings;
  288. if (empty($ts)){
  289. $ts = time();
  290. }
  291. $this->ts = $ts;
  292. $this->ts_def = $this->ts;
  293. $date_string = date('i::G::H::j::d::t::N::z::L::W::n::m::Y::Z::T::s',$ts);
  294. list($this->minute,$this->hour,$this->z_hour,$this->day,$this->z_day,$this->daysinmonth,$this->dayofweek,$this->dayofyear,$is_leap,$this->week,$this->month,$this->z_month,$this->year,$this->offset,$this->tz,$this->second) = explode('::',$date_string);
  295. $this->dayofweek_inshort =$mod_strings['cal_weekdays_short'][$this->dayofweek-1];
  296. $this->dayofweek_inlong=$mod_strings['cal_weekdays_long'][$this->dayofweek-1];
  297. $this->month_inshort=$mod_strings['cal_month_short'][$this->month];
  298. $this->month_inlong=$mod_strings['cal_month_long'][$this->month];
  299. $this->daysinyear = 365;
  300. if ($is_leap == 1){
  301. $this->daysinyear += 1;
  302. }
  303. }
  304. /**
  305. * function to get values from vt_DateTime object
  306. */
  307. function getDateTime()
  308. {
  309. global $mod_strings;
  310. $hour = 0;
  311. $minute = 0;
  312. $second = 0;
  313. $day = 1;
  314. $month = 1;
  315. $year = 1970;
  316. if ( isset($this->second) && $this->second !== ''){
  317. $second = $this->second;
  318. }
  319. if ( isset($this->minute) && $this->minute !== ''){
  320. $minute = $this->minute;
  321. }
  322. if ( isset($this->hour) && $this->hour !== '')
  323. {
  324. $hour = $this->hour;
  325. }
  326. if ( isset($this->day) && $this->day !== ''){
  327. $day= $this->day;
  328. }
  329. if ( isset($this->month) && $this->month !== ''){
  330. $month = $this->month;
  331. }
  332. if ( isset($this->year) && $this->year !== ''){
  333. $year = $this->year;
  334. }else{
  335. die("year was not set");
  336. }
  337. if(empty($hour) && $hour !== 0) $hour = 0;
  338. $this->ts = mktime($hour,$minute,$second,$month,$day,$year);
  339. $this->setDateTime($this->ts);
  340. }
  341. /**
  342. * function to get mysql formatted date
  343. * return formatted date in string format
  344. */
  345. function get_formatted_date(){
  346. $date = $this->year."-".$this->z_month."-".$this->z_day;
  347. return DateTimeField::convertToUserFormat($date);
  348. }
  349. /**
  350. *
  351. * @return Date
  352. */
  353. function get_DB_formatted_date() {
  354. return $this->year."-".$this->z_month."-".$this->z_day;
  355. }
  356. /**
  357. * function to get mysql formatted time
  358. * return formatted time in string format
  359. */
  360. function get_formatted_time(){
  361. $hour = $this->z_hour;
  362. $min = $this->min;
  363. if(empty($hour)) $hour = '00';
  364. if(empty($min)) $min = '00';
  365. return $hour.':'.$min;
  366. }
  367. /**
  368. * function to get date depends on mode value
  369. * @param string $mode - 'increment' or 'decrement'
  370. * return vt_DateTime obj
  371. */
  372. function get_changed_day($mode){
  373. if($mode == 'increment')
  374. $day = $this->day + 1;
  375. else
  376. $day = $this->day - 1;
  377. $date_data = array('day'=>$day,
  378. 'month'=>$this->month,
  379. 'year'=>$this->year
  380. );
  381. return new vt_DateTime($date_data,true);
  382. }
  383. /**
  384. * function to get changed week depends on mode value
  385. * @param string $mode - 'increment' or 'decrement'
  386. * return vt_DateTime obj
  387. */
  388. function get_first_day_of_changed_week($mode){
  389. $first_day = $this->getThisweekDaysbyIndex(1);
  390. if($mode == 'increment')
  391. $day = $first_day->day + 7;
  392. else
  393. $day = $first_day->day - 7;
  394. $date_data = array('day'=>$day,
  395. 'month'=>$first_day->month,
  396. 'year'=>$first_day->year
  397. );
  398. return new vt_DateTime($date_data,true);
  399. }
  400. /**
  401. * function to get month depends on mode value
  402. * @param string $mode - 'increment' or 'decrement'
  403. * return vt_DateTime obj
  404. */
  405. function get_first_day_of_changed_month($mode){
  406. $tmpDate['day'] = $this->day;
  407. $tmpDate['month'] = $this->month;
  408. $tmpDate['year'] = $this->year;
  409. if(is_array($arr) && !empty($arr)){
  410. $tmpDate['year'] = $arr[0];
  411. $tmpDate['month'] = $arr[1];
  412. $tmpDate['day'] = $arr[2];
  413. }
  414. if($mode == 'increment'){
  415. $month = $tmpDate['month'] + 1;
  416. $year = $tmpDate['year'] ;
  417. }else{
  418. if($tmpDate['month'] == 1){
  419. $month = 12;
  420. $year = $tmpDate['year'] - 1;
  421. }else{
  422. $month = $tmpDate['month'] - 1;
  423. $year = $tmpDate['year'] ;
  424. }
  425. }
  426. $date_data = array(
  427. 'day'=>1,
  428. 'month'=>$month,
  429. 'year'=>$year
  430. );
  431. return new vt_DateTime($date_data,true);
  432. }
  433. /**
  434. * function to get year depends on mode value
  435. * @param string $mode - 'increment' or 'decrement'
  436. * return vt_DateTime obj
  437. */
  438. function get_first_day_of_changed_year($mode){
  439. if($mode == 'increment'){
  440. $year = $this->year + 1;
  441. }else{
  442. $year = $this->year - 1;
  443. }
  444. $date_data = array('day'=>1,
  445. 'month'=>1,
  446. 'year'=>$year
  447. );
  448. return new vt_DateTime($date_data,true);
  449. }
  450. /**
  451. * function to get date string
  452. * return date string
  453. */
  454. function get_date_str(){
  455. $array = Array();
  456. if ( isset( $this->hour) && $this->hour != '')
  457. {
  458. array_push( $array, "hour=".$this->hour);
  459. }
  460. if ( isset( $this->day) && $this->day != '')
  461. {
  462. array_push( $array, "day=".$this->day);
  463. }
  464. if ( isset( $this->month) && $this->month)
  465. {
  466. array_push( $array, "month=".$this->month);
  467. }
  468. if ( isset( $this->year) && $this->year != '')
  469. {
  470. array_push( $array, "year=".$this->year);
  471. }
  472. return ("&".implode('&',$array));
  473. }
  474. /**
  475. * function to get days in month using index
  476. *
  477. * This is the newer version of the function getThismonthDaysbyIndex().
  478. * This should be used whereever possible
  479. *
  480. * @param integer $index - number between 0 to 42
  481. * @param string $day - date
  482. * @param string $month - month
  483. * @param string $year - year
  484. * return vt_DateTime obj $datetimevalue
  485. */
  486. function getThisMonthsDayByIndex($index){
  487. $day = $index;
  488. $month = $this->month;
  489. $year = $this->year;
  490. $month_array = array();
  491. $month_array['day'] = $day;
  492. $month_array['month'] = $month;
  493. $month_array['year'] = $year;
  494. $datetimevalue = new vt_DateTime($month_array,true);
  495. return $datetimevalue;
  496. }
  497. }
  498. ?>