2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition 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 ********************************************************************************/
43 * @deprecated Phased out, should not be used anymore
44 * Use SugarDateTime instead
56 var $day_of_week_short;
57 var $day_of_week_long;
71 * Convert from DB-formatted timedate to DateTimeUtil object
72 * @param string $date_start
73 * @param string $time_start
75 function get_time_start($date_start, $time_start = '')
78 if(empty($time_start)) {
79 list($date_start, $time_start) = explode(' ', $date_start);
83 preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/",$date_start,$match);
85 $time_arr['year'] = $match[1];
86 $time_arr['month'] = $match[2];
87 $time_arr['day'] = $match[3];
89 if ( empty( $time_start) )
91 $time_arr['hour'] = 0;
96 if (preg_match('/^(\d\d*):(\d\d*):(\d\d*)$/',$time_start,$match))
98 $time_arr['hour'] = $match[1];
99 $time_arr['min'] = $match[2];
101 else if ( preg_match('/^(\d\d*):(\d\d*)$/',$time_start,$match))
103 $time_arr['hour'] = $match[1];
104 $time_arr['min'] = $match[2];
107 $gmtdiff = date('Z')-$timedate->adjustmentForUserTimeZone()*60;
108 $time_arr['sec'] = $gmtdiff;
109 return new DateTimeUtil($time_arr,true);
112 function get_time_end( $start_time, $duration_hours,$duration_minutes)
114 if ( empty($duration_hours))
116 $duration_hours = "00";
118 if ( empty($duration_minutes))
120 $duration_minutes = "00";
123 $added_seconds = ($duration_hours * 60 * 60 + $duration_minutes * 60 ) - 1;
126 $time_arr['year'] = $start_time->year;
127 $time_arr['month'] = $start_time->month;
128 $time_arr['day'] = $start_time->day;
129 $time_arr['hour'] = $start_time->hour;
130 $time_arr['min'] = $start_time->min;
131 $time_arr['sec'] = $added_seconds;
132 return new DateTimeUtil($time_arr,true);
136 function get_date_str()
140 if ( isset( $this->hour))
142 array_push( $arr, "hour=".$this->hour);
144 if ( isset( $this->day))
146 array_push( $arr, "day=".$this->day);
148 if ( isset( $this->month))
150 array_push( $arr, "month=".$this->month);
152 if ( isset( $this->year))
154 array_push( $arr, "year=".$this->year);
156 return ("&".implode('&',$arr));
159 function get_tomorrow()
161 $date_arr = array('day'=>($this->day + 1),
162 'month'=>$this->month,
163 'year'=>$this->year);
165 return new DateTimeUtil($date_arr,true);
167 function get_yesterday()
169 $date_arr = array('day'=>($this->day - 1),
170 'month'=>$this->month,
171 'year'=>$this->year);
173 return new DateTimeUtil($date_arr,true);
176 function get_mysql_date()
178 return $this->year."-".$this->zmonth."-".$this->zday;
180 function get_mysql_time()
182 return $this->hour.":".$this->min;
185 function parse_utc_date_time($str)
187 preg_match('/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/',$str,$matches);
191 'month'=>$matches[2],
196 $date_time = new DateTimeUtil($date_arr,true);
198 $date_arr = array('ts'=>$date_time->ts + $date_time->tz_offset);
200 return new DateTimeUtil($date_arr,true);
203 function get_utc_date_time()
205 return gmdate('Ymd\THi', $this->ts)."00Z";
208 function get_first_day_of_last_year()
210 $date_arr = array('day'=>1,
212 'year'=>($this->year - 1));
214 return new DateTimeUtil($date_arr,true);
217 function get_first_day_of_next_year()
219 $date_arr = array('day'=>1,
221 'year'=>($this->year + 1));
223 return new DateTimeUtil($date_arr,true);
227 function get_first_day_of_next_week()
229 $first_day = $this->get_day_by_index_this_week(0);
230 $date_arr = array('day'=>($first_day->day + 7),
231 'month'=>$first_day->month,
232 'year'=>$first_day->year);
234 return new DateTimeUtil($date_arr,true);
237 function get_first_day_of_last_week()
239 $first_day = $this->get_day_by_index_this_week(0);
240 $date_arr = array('day'=>($first_day->day - 7),
241 'month'=>$first_day->month,
242 'year'=>$first_day->year);
244 return new DateTimeUtil($date_arr,true);
246 function get_first_day_of_last_month()
248 if ($this->month == 1)
251 $year = $this->year - 1;
255 $month = $this->month - 1;
256 $year = $this->year ;
258 $date_arr = array('day'=>1,
262 return new DateTimeUtil($date_arr,true);
265 function get_first_day_of_this_month()
267 $month = $this->month;
268 $year = $this->year ;
269 $date_arr = array('day'=>1,
273 return new DateTimeUtil($date_arr,true);
276 function get_first_day_of_next_month()
278 $date_arr = array('day'=>1,
279 'month'=>($this->month + 1),
280 'year'=>$this->year);
281 return new DateTimeUtil($date_arr,true);
285 function fill_in_details()
287 global $mod_strings, $timedate;
295 if ( isset($this->sec))
299 if ( isset($this->min))
303 if ( isset($this->hour))
307 if ( isset($this->day))
311 if ( isset($this->month))
313 $month = $this->month;
315 if ( isset($this->year))
321 sugar_die ("fill_in_details: year was not set");
323 $this->ts = mktime($hour,$min,$sec,$month,$day,$year)+$timedate->adjustmentForUserTimeZone()*60;
324 $this->load_ts($this->ts);
328 function load_ts($timestamp)
330 // global $mod_list_strings;
331 global $current_language;
332 $mod_list_strings = return_mod_list_strings_language($current_language,"Calendar");
333 if ( empty($timestamp))
339 $this->ts = $timestamp;
342 $tdiff = $timedate->adjustmentForUserTimeZone();
343 $date_str = date('i:G:H:j:d:t:w:z:L:W:n:m:Y:Z',$timestamp-$tdiff*60);
350 $this->days_in_month,
359 = explode(':',$date_str);
360 $this->tz_offset = date('Z') - $tdiff * 60;
362 $this->day_of_week_short =$mod_list_strings['dom_cal_weekdays'][$this->day_of_week];
363 $this->day_of_week_long=$mod_list_strings['dom_cal_weekdays_long'][$this->day_of_week];
364 $this->month_short=$mod_list_strings['dom_cal_month'][$this->month];
365 $this->month_long=$mod_list_strings['dom_cal_month_long'][$this->month];
367 $this->days_in_year = 365;
371 $this->days_in_year += 1;
377 function DateTimeUtil($time,$fill_in_details)
379 if (! isset( $time) || count($time) == 0 )
381 $this->load_ts(null);
383 else if ( isset( $time['ts']))
385 $this->load_ts($time['ts']);
387 else if ( isset( $time['date_str']))
389 list($this->year,$this->month,$this->day)=
390 explode("-",$time['date_str']);
391 if ($fill_in_details == true)
393 $this->fill_in_details();
398 if ( isset($time['sec']))
400 $this->sec = $time['sec'];
402 if ( isset($time['min']))
404 $this->min = $time['min'];
406 if ( isset($time['hour']))
408 $this->hour = $time['hour'];
410 if ( isset($time['day']))
412 $this->day = $time['day'];
414 if ( isset($time['week']))
416 $this->week = $time['week'];
418 if ( isset($time['month']))
420 $this->month = $time['month'];
422 if ( isset($time['year']) && $time['year'] >= 1970)
424 $this->year = $time['year'];
431 if ($fill_in_details == true)
433 $this->fill_in_details();
439 function dump_date_info()
441 echo "min:".$this->min."<br>\n";
442 echo "hour:".$this->hour."<br>\n";
443 echo "day:".$this->day."<br>\n";
444 echo "month:".$this->month."<br>\n";
445 echo "year:".$this->year."<br>\n";
451 if ($this->hour > 12)
455 else if ($this->hour == 0)
462 function get_24_hour()
469 if ($this->hour >=12)
486 function get_day_of_week_short()
488 return $this->day_of_week_short;
490 function get_day_of_week()
492 return $this->day_of_week_long;
496 function get_month_name()
498 return $this->month_long;
501 function get_datetime_by_index_today($hour_index)
505 if ( $hour_index < 0 || $hour_index > 23 )
507 sugar_die("hour is outside of range");
510 $arr['hour'] = $hour_index;
512 $arr['day'] = $this->day;
514 $arr['month'] = $this->month;
515 $arr['year'] = $this->year;
517 return new DateTimeUtil($arr,true);
520 function get_hour_end_time()
523 $arr['hour'] = $this->hour;
526 $arr['day'] = $this->day;
528 $arr['month'] = $this->month;
529 $arr['year'] = $this->year;
531 return new DateTimeUtil($arr,true);
534 function get_day_end_time()
540 $arr['day'] = $this->day;
542 $arr['month'] = $this->month;
543 $arr['year'] = $this->year;
545 return new DateTimeUtil($arr,true);
548 function get_day_by_index_this_week($day_index)
552 if ( $day_index < 0 || $day_index > 6 )
554 sugar_die("day is outside of week range");
557 $arr['day'] = $this->day +
558 ($day_index - $this->day_of_week);
560 $arr['month'] = $this->month;
561 $arr['year'] = $this->year;
563 return new DateTimeUtil($arr,true);
565 function get_day_by_index_this_year($month_index)
568 $arr['month'] = $month_index+1;
569 $arr['year'] = $this->year;
570 // wp: Find the last day of the month requested, ensure that is the ceiling of the day param
571 $arr['day'] = min(strftime("%d", mktime(0, 0, 0, $arr['month']+1, 0, $arr['year'])), $this->day);
573 return new DateTimeUtil($arr,true);
576 function get_day_by_index_this_month($day_index)
579 $arr['day'] = $day_index + 1;
580 $arr['month'] = $this->month;
581 $arr['year'] = $this->year;
583 return new DateTimeUtil($arr,true);
586 function getHashList($view, &$start_time, &$end_time)
588 $hash_list = array();
590 if (version_compare(phpversion(), '5.0') < 0)
591 $new_time = $start_time;
593 $new_time = clone($start_time);
599 $end_time = $end_time->get_day_end_time();
603 if (empty($new_time->ts))
608 if ( $new_time->ts == $end_time->ts)
613 while( $new_time->ts < $end_time->ts)
616 $arr['month'] = $new_time->month;
617 $arr['year'] = $new_time->year;
618 $arr['day'] = $new_time->day;
619 $arr['hour'] = $new_time->hour;
622 $hash_list[] = $new_time->get_mysql_date().":".$new_time->hour;
627 $hash_list[] = $new_time->get_mysql_date();
630 $new_time = new DateTimeUtil($arr,true);