/** * @param integer $duration * @param string|null $start_date The starting date in format of Y-m-d * @return array */ public function getGenericStartEndByDuration($duration, $start_date = null) { $mapping = array('current' => 0, 'next' => 3, 'year' => 12); if (array_key_exists($duration, $mapping)) { $duration = $mapping[$duration]; } elseif (!is_numeric($duration)) { $duration = 0; } $start = false; if (!is_null($start_date)) { $start = SugarDateTime::createFromFormat('Y-m-d', $start_date); $end = SugarDateTime::createFromFormat('Y-m-d', $start_date); } if ($start === false) { $start = new SugarDateTime(); $end = new SugarDateTime(); } // since we subtract one from the month, we need to add one to the duration since php is // not zero 0 based for months // figure out what the starting month is. $startMonth = floor(($start->month - 1) / 3) * 3 + $duration + 1; $year = $start->year; $endYear = $year; $endMonth = $startMonth + 3; // if the end month is dec, we put it to Jan and increase the end year as well so it goes back to the last // day of dec if ($endMonth == 12) { $endYear++; $endMonth = 1; } if ($duration == 12) { $endYear++; $startMonth = 1; $endMonth = 1; } $start->setDate($year, $startMonth, 1); $end->setDate($endYear, $endMonth, 0); // since we are using timestamp, we need to convert this into UTC since that is // what the DB is storing as $tz = new DateTimeZone("UTC"); return array('start_date' => $start->asDbDate(false), 'start_date_timestamp' => $start->setTimezone($tz)->setTime(0, 0, 0)->format('U'), 'end_date' => $end->asDbDate(false), 'end_date_timestamp' => $end->setTimezone($tz)->setTime(0, 0, 0)->format('U')); }
/** * @deprecated for public use * handles offset values for Timezones and DST * @param $date string date/time formatted in user's selected format * @param $format string destination format value as passed to PHP's date() funtion * @param $to boolean * @param $user object user object from which Timezone and DST * @param $usetimezone string timezone name * values will be derived * @return string date formatted and adjusted for TZ and DST */ function handle_offset($date, $format, $to = true, $user = null, $usetimezone = null) { $tz = empty($usetimezone) ? $this->_getUserTZ($user) : new DateTimeZone($usetimezone); $dateobj = new SugarDateTime($date, $to ? self::$gmtTimezone : $tz); $dateobj->setTimezone($to ? $tz : self::$gmtTimezone); return $dateobj->format($format); // return $this->_convert($date, $format, $to ? self::$gmtTimezone : $tz, $format, $to ? $tz : self::$gmtTimezone); }
/** * Set the DateTime Search Data based on Current User TimeZone * * @param string $userSearchDateTime - user Search Datetime * @return string $dbSearchDateTime - database Search Datetime */ public function toDatabaseSearchDateTime($userSearchDateTime) { global $timedate; global $current_user; $usertimezone = $current_user->getPreference('timezone'); if (empty($usertimezone)) { $usertimezone = "UTC"; } $tz = new DateTimeZone($usertimezone); $sugarDateTime = new SugarDateTime($userSearchDateTime); $sugarDateTime->setTimezone($tz); $dbSearchDateTime = $timedate->asDb($sugarDateTime); return $dbSearchDateTime; }