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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
40 ********************************************************************************/
52 var $day_of_week_short;
53 var $day_of_week_long;
67 * Convert from DB-formatted timedate to DateTimeUtil object
68 * @param string $date_start
69 * @param string $time_start
71 function get_time_start($date_start, $time_start = '')
74 if(empty($time_start)) {
75 list($date_start, $time_start) = explode(' ', $date_start);
79 preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$date_start,$match);
81 $time_arr['year'] = $match[1];
82 $time_arr['month'] = $match[2];
83 $time_arr['day'] = $match[3];
85 if ( empty( $time_start) )
87 $time_arr['hour'] = 0;
92 if (preg_match('/^(\d\d*):(\d\d*):(\d\d*)$/',$time_start,$match))
94 $time_arr['hour'] = $match[1];
95 $time_arr['min'] = $match[2];
97 else if ( preg_match('/^(\d\d*):(\d\d*)$/',$time_start,$match))
99 $time_arr['hour'] = $match[1];
100 $time_arr['min'] = $match[2];
103 $gmtdiff = date('Z')-$timedate->adjustmentForUserTimeZone()*60;
104 $time_arr['sec'] = $gmtdiff;
105 return new DateTimeUtil($time_arr,true);
108 function get_time_end( $start_time, $duration_hours,$duration_minutes)
110 if ( empty($duration_hours))
112 $duration_hours = "00";
114 if ( empty($duration_minutes))
116 $duration_minutes = "00";
119 $added_seconds = ($duration_hours * 60 * 60 + $duration_minutes * 60 ) - 1;
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);
132 function get_date_str()
136 if ( isset( $this->hour))
138 array_push( $arr, "hour=".$this->hour);
140 if ( isset( $this->day))
142 array_push( $arr, "day=".$this->day);
144 if ( isset( $this->month))
146 array_push( $arr, "month=".$this->month);
148 if ( isset( $this->year))
150 array_push( $arr, "year=".$this->year);
152 return ("&".implode('&',$arr));
155 function get_tomorrow()
157 $date_arr = array('day'=>($this->day + 1),
158 'month'=>$this->month,
159 'year'=>$this->year);
161 return new DateTimeUtil($date_arr,true);
163 function get_yesterday()
165 $date_arr = array('day'=>($this->day - 1),
166 'month'=>$this->month,
167 'year'=>$this->year);
169 return new DateTimeUtil($date_arr,true);
172 function get_mysql_date()
174 return $this->year."-".$this->zmonth."-".$this->zday;
176 function get_mysql_time()
178 return $this->hour.":".$this->min;
181 function parse_utc_date_time($str)
183 preg_match('/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/',$str,$matches);
187 'month'=>$matches[2],
192 $date_time = new DateTimeUtil($date_arr,true);
194 $date_arr = array('ts'=>$date_time->ts + $date_time->tz_offset);
196 return new DateTimeUtil($date_arr,true);
199 function get_utc_date_time()
201 return gmdate('Ymd\THi', $this->ts)."00Z";
204 function get_first_day_of_last_year()
206 $date_arr = array('day'=>1,
208 'year'=>($this->year - 1));
210 return new DateTimeUtil($date_arr,true);
213 function get_first_day_of_next_year()
215 $date_arr = array('day'=>1,
217 'year'=>($this->year + 1));
219 return new DateTimeUtil($date_arr,true);
223 function get_first_day_of_next_week()
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);
230 return new DateTimeUtil($date_arr,true);
233 function get_first_day_of_last_week()
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);
240 return new DateTimeUtil($date_arr,true);
242 function get_first_day_of_last_month()
244 if ($this->month == 1)
247 $year = $this->year - 1;
251 $month = $this->month - 1;
252 $year = $this->year ;
254 $date_arr = array('day'=>1,
258 return new DateTimeUtil($date_arr,true);
261 function get_first_day_of_this_month()
263 $month = $this->month;
264 $year = $this->year ;
265 $date_arr = array('day'=>1,
269 return new DateTimeUtil($date_arr,true);
272 function get_first_day_of_next_month()
274 $date_arr = array('day'=>1,
275 'month'=>($this->month + 1),
276 'year'=>$this->year);
277 return new DateTimeUtil($date_arr,true);
281 function fill_in_details()
283 global $mod_strings, $timedate;
291 if ( isset($this->sec))
295 if ( isset($this->min))
299 if ( isset($this->hour))
303 if ( isset($this->day))
307 if ( isset($this->month))
309 $month = $this->month;
311 if ( isset($this->year))
317 sugar_die ("fill_in_details: year was not set");
319 $this->ts = mktime($hour,$min,$sec,$month,$day,$year)+$timedate->adjustmentForUserTimeZone()*60;
320 $this->load_ts($this->ts);
324 function load_ts($timestamp)
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))
335 $this->ts = $timestamp;
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);
346 $this->days_in_month,
355 = explode(':',$date_str);
356 $this->tz_offset = date('Z') - $tdiff * 60;
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];
363 $this->days_in_year = 365;
367 $this->days_in_year += 1;
373 function DateTimeUtil($time,$fill_in_details)
375 if (! isset( $time) || count($time) == 0 )
377 $this->load_ts(null);
379 else if ( isset( $time['ts']))
381 $this->load_ts($time['ts']);
383 else if ( isset( $time['date_str']))
385 list($this->year,$this->month,$this->day)=
386 explode("-",$time['date_str']);
387 if ($fill_in_details == true)
389 $this->fill_in_details();
394 if ( isset($time['sec']))
396 $this->sec = $time['sec'];
398 if ( isset($time['min']))
400 $this->min = $time['min'];
402 if ( isset($time['hour']))
404 $this->hour = $time['hour'];
406 if ( isset($time['day']))
408 $this->day = $time['day'];
410 if ( isset($time['week']))
412 $this->week = $time['week'];
414 if ( isset($time['month']))
416 $this->month = $time['month'];
418 if ( isset($time['year']) && $time['year'] >= 1970)
420 $this->year = $time['year'];
427 if ($fill_in_details == true)
429 $this->fill_in_details();
435 function dump_date_info()
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";
447 if ($this->hour > 12)
451 else if ($this->hour == 0)
458 function get_24_hour()
465 if ($this->hour >=12)
482 function get_day_of_week_short()
484 return $this->day_of_week_short;
486 function get_day_of_week()
488 return $this->day_of_week_long;
492 function get_month_name()
494 return $this->month_long;
497 function get_datetime_by_index_today($hour_index)
501 if ( $hour_index < 0 || $hour_index > 23 )
503 sugar_die("hour is outside of range");
506 $arr['hour'] = $hour_index;
508 $arr['day'] = $this->day;
510 $arr['month'] = $this->month;
511 $arr['year'] = $this->year;
513 return new DateTimeUtil($arr,true);
516 function get_hour_end_time()
519 $arr['hour'] = $this->hour;
522 $arr['day'] = $this->day;
524 $arr['month'] = $this->month;
525 $arr['year'] = $this->year;
527 return new DateTimeUtil($arr,true);
530 function get_day_end_time()
536 $arr['day'] = $this->day;
538 $arr['month'] = $this->month;
539 $arr['year'] = $this->year;
541 return new DateTimeUtil($arr,true);
544 function get_day_by_index_this_week($day_index)
548 if ( $day_index < 0 || $day_index > 6 )
550 sugar_die("day is outside of week range");
553 $arr['day'] = $this->day +
554 ($day_index - $this->day_of_week);
556 $arr['month'] = $this->month;
557 $arr['year'] = $this->year;
559 return new DateTimeUtil($arr,true);
561 function get_day_by_index_this_year($month_index)
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);
569 return new DateTimeUtil($arr,true);
572 function get_day_by_index_this_month($day_index)
575 $arr['day'] = $day_index + 1;
576 $arr['month'] = $this->month;
577 $arr['year'] = $this->year;
579 return new DateTimeUtil($arr,true);
582 function getHashList($view, &$start_time, &$end_time)
584 $hash_list = array();
586 if (version_compare(phpversion(), '5.0') < 0)
587 $new_time = $start_time;
589 $new_time = clone($start_time);
595 $end_time = $end_time->get_day_end_time();
599 if (empty($new_time->ts))
604 if ( $new_time->ts == $end_time->ts)
609 while( $new_time->ts < $end_time->ts)
612 $arr['month'] = $new_time->month;
613 $arr['year'] = $new_time->year;
614 $arr['day'] = $new_time->day;
615 $arr['hour'] = $new_time->hour;
618 $hash_list[] = $new_time->get_mysql_date().":".$new_time->hour;
623 $hash_list[] = $new_time->get_mysql_date();
626 $new_time = new DateTimeUtil($arr,true);