]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Calendar/DateTimeUtil.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Calendar / DateTimeUtil.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  ********************************************************************************/
41
42 class DateTimeUtil
43 {
44                 var $timezone;
45                 var $sec;
46                 var $min;
47                 var $hour;
48                 var $zhour;
49                 var $day;
50                 var $zday;
51                 var $day_of_week;
52                 var $day_of_week_short;
53                 var $day_of_week_long;
54                 var $day_of_year;
55                 var $week;
56                 var $month;
57                 var $zmonth;
58                 var $month_short;
59                 var $month_long;
60                 var $year;
61                 var $am_pm;
62                 var $tz_offset;
63
64                 // unix epoch time
65                 var $ts;
66     /**
67      * Convert from DB-formatted timedate to DateTimeUtil object
68      * @param string $date_start
69      * @param string $time_start
70      */
71     function get_time_start($date_start, $time_start = '')
72         {
73                 global $timedate;
74                 if(empty($time_start)) {
75                         list($date_start, $time_start) = explode(' ', $date_start);
76                 }
77                 $match=array();
78
79                 preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$date_start,$match);
80                 $time_arr = array();
81                 $time_arr['year'] = $match[1];
82                 $time_arr['month'] = $match[2];
83                 $time_arr['day'] = $match[3];
84
85                 if ( empty( $time_start) )
86                 {
87                         $time_arr['hour'] = 0;
88                         $time_arr['min'] = 0;
89                 }
90                 else
91                 {
92                         if (preg_match('/^(\d\d*):(\d\d*):(\d\d*)$/',$time_start,$match))
93                         {
94                                 $time_arr['hour'] = $match[1];
95                                 $time_arr['min'] = $match[2];
96                         }
97                         else if ( preg_match('/^(\d\d*):(\d\d*)$/',$time_start,$match))
98                         {
99                                 $time_arr['hour'] = $match[1];
100                                 $time_arr['min'] = $match[2];
101                         }
102                 }
103                 $gmtdiff = date('Z')-$timedate->adjustmentForUserTimeZone()*60;
104                 $time_arr['sec'] = $gmtdiff;
105                 return new DateTimeUtil($time_arr,true);
106         }
107
108         function get_time_end( $start_time, $duration_hours,$duration_minutes)
109         {
110                 if ( empty($duration_hours))
111                 {
112                         $duration_hours = "00";
113                 }
114                 if ( empty($duration_minutes))
115                 {
116                         $duration_minutes = "00";
117                 }
118
119                 $added_seconds = ($duration_hours * 60 * 60 + $duration_minutes * 60 ) - 1;
120
121                 $time_arr = array();
122                 $time_arr['year'] = $start_time->year;
123                 $time_arr['month'] = $start_time->month;
124                 $time_arr['day'] = $start_time->day;
125                 $time_arr['hour'] = $start_time->hour;
126                 $time_arr['min'] = $start_time->min;
127                 $time_arr['sec'] = $added_seconds;
128                 return new DateTimeUtil($time_arr,true);
129
130         }
131
132         function get_date_str()
133         {
134
135                 $arr = array();
136                 if ( isset( $this->hour))
137                 {
138                  array_push( $arr, "hour=".$this->hour);
139                 }
140                 if ( isset( $this->day))
141                 {
142                  array_push( $arr, "day=".$this->day);
143                 }
144                 if ( isset( $this->month))
145                 {
146                  array_push( $arr, "month=".$this->month);
147                 }
148                 if ( isset( $this->year))
149                 {
150                  array_push( $arr, "year=".$this->year);
151                 }
152                 return  ("&".implode('&',$arr));
153         }
154
155         function get_tomorrow()
156         {
157                         $date_arr = array('day'=>($this->day + 1),
158                         'month'=>$this->month,
159                         'year'=>$this->year);
160
161                 return new DateTimeUtil($date_arr,true);
162         }
163         function get_yesterday()
164         {
165                         $date_arr = array('day'=>($this->day - 1),
166                         'month'=>$this->month,
167                         'year'=>$this->year);
168
169                 return new DateTimeUtil($date_arr,true);
170         }
171
172         function get_mysql_date()
173         {
174                 return $this->year."-".$this->zmonth."-".$this->zday;
175         }
176         function get_mysql_time()
177         {
178                 return $this->hour.":".$this->min;
179         }
180
181   function parse_utc_date_time($str)
182   {
183     preg_match('/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/',$str,$matches);
184
185     $date_arr = array(
186       'year'=>$matches[1],
187       'month'=>$matches[2],
188       'day'=>$matches[3],
189       'hour'=>$matches[4],
190       'min'=>$matches[5]);
191
192       $date_time = new DateTimeUtil($date_arr,true);
193
194       $date_arr = array('ts'=>$date_time->ts + $date_time->tz_offset);
195
196       return new DateTimeUtil($date_arr,true);
197   }
198
199         function get_utc_date_time()
200         {
201                 return gmdate('Ymd\THi', $this->ts)."00Z";
202         }
203
204         function get_first_day_of_last_year()
205         {
206                         $date_arr = array('day'=>1,
207                         'month'=>1,
208                         'year'=>($this->year - 1));
209
210                 return new DateTimeUtil($date_arr,true);
211
212         }
213         function get_first_day_of_next_year()
214         {
215                         $date_arr = array('day'=>1,
216                         'month'=>1,
217                         'year'=>($this->year + 1));
218
219                 return new DateTimeUtil($date_arr,true);
220
221         }
222
223         function get_first_day_of_next_week()
224         {
225                 $first_day = $this->get_day_by_index_this_week(0);
226                         $date_arr = array('day'=>($first_day->day + 7),
227                         'month'=>$first_day->month,
228                         'year'=>$first_day->year);
229
230                 return new DateTimeUtil($date_arr,true);
231
232         }
233         function get_first_day_of_last_week()
234         {
235                 $first_day = $this->get_day_by_index_this_week(0);
236                         $date_arr = array('day'=>($first_day->day - 7),
237                         'month'=>$first_day->month,
238                         'year'=>$first_day->year);
239
240                 return new DateTimeUtil($date_arr,true);
241         }
242         function get_first_day_of_last_month()
243         {
244                 if ($this->month == 1)
245                 {
246                         $month = 12;
247                         $year = $this->year - 1;
248                 }
249                 else
250                 {
251                         $month = $this->month - 1;
252                         $year = $this->year ;
253                 }
254                         $date_arr = array('day'=>1,
255                         'month'=>$month,
256                         'year'=>$year);
257
258                 return new DateTimeUtil($date_arr,true);
259
260         }
261         function get_first_day_of_this_month()
262         {
263                 $month = $this->month;
264                 $year = $this->year ;
265                 $date_arr = array('day'=>1,
266                 'month'=>$month,
267                 'year'=>$year);
268
269                 return new DateTimeUtil($date_arr,true);
270
271         }
272         function get_first_day_of_next_month()
273         {
274                 $date_arr = array('day'=>1,
275                         'month'=>($this->month + 1),
276                         'year'=>$this->year);
277                 return new DateTimeUtil($date_arr,true);
278         }
279
280
281         function fill_in_details()
282         {
283                 global $mod_strings, $timedate;
284                 $hour = 0;
285                 $min = 0;
286                 $sec = 0;
287                 $day = 1;
288                 $month = 1;
289                 $year = 1970;
290
291                 if ( isset($this->sec))
292                 {
293                         $sec = $this->sec;
294                 }
295                 if ( isset($this->min))
296                 {
297                         $min = $this->min;
298                 }
299                 if ( isset($this->hour))
300                 {
301                         $hour = $this->hour;
302                 }
303                 if ( isset($this->day))
304                 {
305                         $day= $this->day;
306                 }
307                 if ( isset($this->month))
308                 {
309                         $month = $this->month;
310                 }
311                 if ( isset($this->year))
312                 {
313                         $year = $this->year;
314                 }
315                 else
316                 {
317                         sugar_die ("fill_in_details: year was not set");
318                 }
319                 $this->ts = mktime($hour,$min,$sec,$month,$day,$year)+$timedate->adjustmentForUserTimeZone()*60;
320                 $this->load_ts($this->ts);
321
322         }
323
324         function load_ts($timestamp)
325         {
326         //      global $mod_list_strings;
327                 global $current_language;
328                 $mod_list_strings = return_mod_list_strings_language($current_language,"Calendar");
329                 if ( empty($timestamp))
330                 {
331
332                         $timestamp = time();
333                 }
334
335                 $this->ts = $timestamp;
336                 global $timedate;
337
338                 $tdiff = $timedate->adjustmentForUserTimeZone();
339                 $date_str = date('i:G:H:j:d:t:w:z:L:W:n:m:Y:Z',$timestamp-$tdiff*60);
340                 list(
341                 $this->min,
342                 $this->hour,
343                 $this->zhour,
344                 $this->day,
345                 $this->zday,
346                 $this->days_in_month,
347                 $this->day_of_week,
348                 $this->day_of_year,
349                 $is_leap,
350                 $this->week,
351                 $this->month,
352                 $this->zmonth,
353                 $this->year,
354                 $this->tz_offset)
355                  = explode(':',$date_str);
356                 $this->tz_offset = date('Z') - $tdiff * 60;
357
358                 $this->day_of_week_short =$mod_list_strings['dom_cal_weekdays'][$this->day_of_week];
359                 $this->day_of_week_long=$mod_list_strings['dom_cal_weekdays_long'][$this->day_of_week];
360                 $this->month_short=$mod_list_strings['dom_cal_month'][$this->month];
361                 $this->month_long=$mod_list_strings['dom_cal_month_long'][$this->month];
362
363                 $this->days_in_year = 365;
364
365                 if ($is_leap == 1)
366                 {
367                         $this->days_in_year += 1;
368                 }
369
370
371         }
372
373         function DateTimeUtil($time,$fill_in_details)
374         {
375                 if (! isset( $time) || count($time) == 0 )
376                 {
377                         $this->load_ts(null);
378                 }
379                 else if ( isset( $time['ts']))
380                 {
381                         $this->load_ts($time['ts']);
382                 }
383                 else if ( isset( $time['date_str']))
384                 {
385                         list($this->year,$this->month,$this->day)=
386                                 explode("-",$time['date_str']);
387                         if ($fill_in_details == true)
388                         {
389                                 $this->fill_in_details();
390                         }
391                 }
392                 else
393                 {
394                         if ( isset($time['sec']))
395                         {
396                                 $this->sec = $time['sec'];
397                         }
398                         if ( isset($time['min']))
399                         {
400                                 $this->min = $time['min'];
401                         }
402                         if ( isset($time['hour']))
403                         {
404                                 $this->hour = $time['hour'];
405                         }
406                         if ( isset($time['day']))
407                         {
408                                 $this->day = $time['day'];
409                         }
410                         if ( isset($time['week']))
411                         {
412                                 $this->week = $time['week'];
413                         }
414                         if ( isset($time['month']))
415                         {
416                                 $this->month = $time['month'];
417                         }
418                         if ( isset($time['year']) && $time['year'] >= 1970)
419                         {
420                                 $this->year = $time['year'];
421                         }
422                         else
423                         {
424                                 return null;
425                         }
426
427                         if ($fill_in_details == true)
428                         {
429                                 $this->fill_in_details();
430                         }
431
432                 }
433         }
434
435         function dump_date_info()
436         {
437                 echo "min:".$this->min."<br>\n";
438                 echo "hour:".$this->hour."<br>\n";
439                 echo "day:".$this->day."<br>\n";
440                 echo "month:".$this->month."<br>\n";
441                 echo "year:".$this->year."<br>\n";
442         }
443
444         function get_hour()
445         {
446                 $hour = $this->hour;
447                 if ($this->hour > 12)
448                 {
449                         $hour -= 12;
450                 }
451                 else if ($this->hour == 0)
452                 {
453                         $hour = 12;
454                 }
455                 return $hour;
456         }
457
458         function get_24_hour()
459         {
460                 return $this->hour;
461         }
462
463         function get_am_pm()
464         {
465                 if ($this->hour >=12)
466                 {
467                         return "PM";
468                 }
469                 return "AM";
470         }
471
472         function get_day()
473         {
474                 return $this->day;
475         }
476
477         function get_month()
478         {
479                 return $this->month;
480         }
481
482         function get_day_of_week_short()
483         {
484                 return $this->day_of_week_short;
485         }
486         function get_day_of_week()
487         {
488                 return $this->day_of_week_long;
489         }
490
491
492         function get_month_name()
493         {
494                 return $this->month_long;
495         }
496
497         function get_datetime_by_index_today($hour_index)
498         {
499                 $arr = array();
500
501                 if ( $hour_index < 0 || $hour_index > 23  )
502                 {
503                         sugar_die("hour is outside of range");
504                 }
505
506                 $arr['hour'] = $hour_index;
507                 $arr['min'] = 0;
508                 $arr['day'] = $this->day;
509
510                 $arr['month'] = $this->month;
511                 $arr['year'] = $this->year;
512
513                 return new DateTimeUtil($arr,true);
514         }
515
516         function get_hour_end_time()
517         {
518                 $arr = array();
519                 $arr['hour'] = $this->hour;
520                 $arr['min'] = 59;
521                 $arr['sec'] = 59;
522                 $arr['day'] = $this->day;
523
524                 $arr['month'] = $this->month;
525                 $arr['year'] = $this->year;
526
527                 return new DateTimeUtil($arr,true);
528         }
529
530         function get_day_end_time()
531         {
532                 $arr = array();
533                 $arr['hour'] = 23;
534                 $arr['min'] = 59;
535                 $arr['sec'] = 59;
536                 $arr['day'] = $this->day;
537
538                 $arr['month'] = $this->month;
539                 $arr['year'] = $this->year;
540
541                 return new DateTimeUtil($arr,true);
542         }
543
544         function get_day_by_index_this_week($day_index)
545         {
546                 $arr = array();
547
548                 if ( $day_index < 0 || $day_index > 6  )
549                 {
550                         sugar_die("day is outside of week range");
551                 }
552
553                 $arr['day'] = $this->day +
554                         ($day_index - $this->day_of_week);
555
556                 $arr['month'] = $this->month;
557                 $arr['year'] = $this->year;
558
559                 return new DateTimeUtil($arr,true);
560         }
561         function get_day_by_index_this_year($month_index)
562         {
563                 $arr = array();
564                 $arr['month'] = $month_index+1;
565                 $arr['year'] = $this->year;
566                 // wp: Find the last day of the month requested, ensure that is the ceiling of the day param
567                 $arr['day'] = min(strftime("%d", mktime(0, 0, 0, $arr['month']+1, 0, $arr['year'])), $this->day);
568
569                 return new DateTimeUtil($arr,true);
570         }
571
572         function get_day_by_index_this_month($day_index)
573         {
574                 $arr = array();
575                 $arr['day'] = $day_index + 1;
576                 $arr['month'] = $this->month;
577                 $arr['year'] = $this->year;
578
579                 return new DateTimeUtil($arr,true);
580         }
581
582         function getHashList($view, &$start_time, &$end_time)
583         {
584                 $hash_list = array();
585
586         if (version_compare(phpversion(), '5.0') < 0)
587             $new_time = $start_time;
588         else
589             $new_time = clone($start_time);
590
591                 $arr = array();
592
593                 if ( $view != 'day')
594                 {
595                   $end_time = $end_time->get_day_end_time();
596                 }
597
598
599                 if (empty($new_time->ts))
600                 {
601                         return;
602                 }
603
604                 if ( $new_time->ts == $end_time->ts)
605                 {
606                         $end_time->ts+=1;
607                 }
608
609                  while( $new_time->ts < $end_time->ts)
610                  {
611
612                   $arr['month'] = $new_time->month;
613                   $arr['year'] = $new_time->year;
614                   $arr['day'] = $new_time->day;
615                   $arr['hour'] = $new_time->hour;
616                   if ( $view == 'day')
617                   {
618                    $hash_list[] = $new_time->get_mysql_date().":".$new_time->hour;
619                    $arr['hour'] += 1;
620                   }
621                   else
622                   {
623                    $hash_list[] = $new_time->get_mysql_date();
624                    $arr['day'] += 1;
625                   }
626                   $new_time = new DateTimeUtil($arr,true);
627     }
628                 return $hash_list;
629         }
630
631 }
632
633 ?>