Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Joomla\Registry\Registry;
- use Psr\Log\InvalidArgumentException;
- use Joomla\String\String;
- /**
- * @version $id$
- * @package com_cn_reports
- * @copyright Copyright (C) 2015. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE.txt
- * @author
- */
- // No direct access
- defined('_JEXEC') or die;
- /**
- * Cn_reports helper.
- */
- abstract class cnreportDateHelper
- {
- /**
- * Gets the date offset for timezone as UTC/GMT.
- * If the timezone offset is a negative value then the return value is less than the
- * UTC/GMT value.<br />Otherwise the return date is ahead to the UTC/GMT by the amount of the
- * timezone offset.
- * @param JDate $objDate the input date
- * @return JData UTC/GMT date set by the offset of $objDate
- * @throws InvalidArgumentException
- * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
- * $displayDate = cnreportDateHelper::getDateTimeFromUtc($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 11:00:00 PM displays as UTC + 10h<br />
- * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
- * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 05:00:00 PM displays as UTC + 4h<br />
- * $displayDate = cnreportDateHelper::getDateTimeFromUtc($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 09:00:00 AM PM displays as UTC - 4h<br />
- */
- public static function getDateTimeFromUtc($objDate=null)
- {
- if (!is_null($objDate)) {
- if (is_object($objDate) && (!($objDate instanceof JDate)))
- {
- throw new InvalidArgumentException('$objDate not set to an instace of JDate');
- }
- if (!is_object($objDate)) {
- $objDate = JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
- }
- } else {
- $objDate = JFactory::getDate('now', cnreportDateHelper::getOffset());
- }
- $offSec = $objDate->getOffsetFromGMT();
- if ($offSec == 0) {
- return clone $objDate;
- }
- // create a new date in UTC/GMT from $objDate using
- // the UTC/GMT fo the $objDate
- $retval = JFactory::getDate($objDate->format('Y-m-d H:i:s', true), 'UTC');
- $sint = 'PT' . abs($offSec) .'S'; // formated for time with seconds
- $interval = new DateInterval($sint);
- // if the timezone offset is negative then invert the DateInterval
- // this will allow the time to be subtraced rather than added
- if ($offSec < 0) {
- $interval->invert =1;
- }
- // add the interval
- $retval->add($interval);
- return $retval;
- }
- /**
- * Gets the Start of the day from the date passed in adjusted to the timezone of date.
- * In other wordes set the date to 12:00:00 AM of the same day and the applies the timezone.<br />
- * If the timezone offset is a negative value then the return value is less than the
- * UTC/GMT value.<br />Otherwise the return date is ahead to the UTC/GMT by the amount of the
- * timezone offset.
- * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
- * @throws InvalidArgumentException
- * @return JDate UTC/GMT date set to the start of the day plus timezone offset
- * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
- * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 10:00:00 AM displays as 12:00 AM UTC + 10h<br />
- * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
- * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC + 4h<br />
- * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-01 08:00:00 PM displays as 12:00:00 AM UTC - 4h<br />
- */
- public static function getDateStartFromUtc($objDate = null)
- {
- $retval = null;
- if (!is_null($objDate)) {
- if (is_object($objDate) && (!($objDate instanceof JDate)))
- {
- throw new InvalidArgumentException('$objDate not set to an instace of JDate');
- }
- $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
- } else {
- $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
- }
- $retval->setTime(0, 0, 0);
- $retval = cnreportDateHelper::getDateTimeFromUtc($retval);
- return $retval;
- }
- /**
- * Gets the End of the day from the date passed in adjusted to the timezone of date.
- * In other wordes set the date to 11:59:59 PM of the same day and the applies the timezone.<br />
- * If the timezone offset is a negative value then the return value is less than the
- * UTC/GMT value.<br />Otherwise the return date is ahead to the UTC/GMT by the amount of the
- * timezone offset.
- * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
- * @throws InvalidArgumentException
- * @return JDate UTC/GMT date set to the start of the day plus timezone offset
- * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
- * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-03 09:59:59 AM displays as 11:59:59 PM UTC + 10h<br />
- * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
- * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC + 4h<br />
- * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 07:59:59 PM displays as 11:59:59 PM UTC - 4h<br />
- */
- public static function getDateEndFromUtc($objDate = null)
- {
- $retval = null;
- if (!is_null($objDate)) {
- if (is_object($objDate) && (!($objDate instanceof JDate)))
- {
- throw new InvalidArgumentException('$objDate not set to an instace of JDate');
- }
- $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
- } else {
- $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
- }
- $retval->setTime(23, 59, 59);
- $retval = cnreportDateHelper::getDateTimeFromUtc($retval);
- return $retval;
- }
- /**
- * Gets the Start of the day from the date passed in adjusted to the timezone of date.
- * In other wordes set the date to 12:00:00 AM of the same day and the applies the timezone.<br />
- * If the timezone offset is a negative value then the return value is greater than the
- * UTC/GMT value.<br />Otherwise the return date is behind to the UTC/GMT by the amount of the
- * timezone offset.<br />
- * This is the same format used by Joomla. By Default Negative Timezone offsets are added to the time and a positive
- * and Postitive Timezone offsets are subtracted from the time.
- * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
- * @throws InvalidArgumentException
- * @return JDate UTC/GMT date set to the start of the day plus timezone offset
- * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
- * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-01 02:00:00 PM displays as 12:00 AM UTC - 10h<br />
- * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
- * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); //2015-08-02 03:00:00 AM displays as UTC + 4h<br />
- * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 04:00:00 AM displays as 12:00:00 AM UTC + 4h<br />
- */
- public static function getDateStart($objDate = null)
- {
- $retval = null;
- if (!is_null($objDate)) {
- if (is_object($objDate) && (!($objDate instanceof JDate)))
- {
- throw new InvalidArgumentException('$objDate not set to an instace of JDate');
- }
- $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
- } else {
- $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
- }
- $retval->setTime(0, 0, 0);
- // Now change the timezone back to UTC.
- $tz = new DateTimeZone('GMT');
- $retval->setTimezone($tz);
- return $retval;
- }
- /**
- * Gets the End of the day from the date passed in adjusted to the timezone of date.
- * In other wordes set the date to 12:00:00 AM of the same day and the applies the timezone.<br />
- * If the timezone offset is a negative value then the return value is greater than the
- * UTC/GMT value.<br />Otherwise the return date is behind to the UTC/GMT by the amount of the
- * timezone offset.<br />
- * This is the same format used by Joomla. By Default Negative Timezone offsets are added to the time and a positive
- * and Postitive Timezone offsets are subtracted from the time.
- * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
- * @throws InvalidArgumentException
- * @return JDate UTC/GMT date set to the start of the day plus timezone offset
- * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
- * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 01:59:59 PM displays as 12:00 AM UTC - 10h<br />
- * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
- * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
- * echo $mydate->format('Y-m-d h:i:s A', false); //2015-08-02 03:00:00 AM displays as UTC + 4h<br />
- * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
- * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-03 03:59:59 AM AM displays as 23:59:59 AM UTC + 4h<br />
- */
- public static function getDateEnd($objDate = null)
- {
- $retval = null;
- if (!is_null($objDate)) {
- if (is_object($objDate) && (!($objDate instanceof JDate)))
- {
- throw new InvalidArgumentException('$objDate not set to an instace of JDate');
- }
- $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
- } else {
- $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
- }
- $retval->setTime(23, 59, 59);
- $tz = new DateTimeZone('GMT');
- $retval->setTimezone($tz);
- return $retval;
- }
- /**
- * Helper method to get Joomlas Timezone
- * @return String contain timezone such sucha as 'America/Toronto'
- */
- public static function getOffset() {
- $config = JFactory::getConfig();
- $jtz = $config->get('offset');
- return $jtz;
- }
- /**
- * Returns the userTime zone if the user has set one, or the global config one
- * @return DateTimeZone
- */
- public static function getTimeZoneUser() {
- $user = JFactory::getUser();
- $userTz = $user->getParam('timezone');
- $config = JFactory::getConfig();
- $timeZone = $config->get('offset');
- if($userTz) {
- $timeZone = $userTz;
- }
- return new DateTimeZone($timeZone);
- }
- /**
- * Returns the the global timezone
- * @return DateTimeZone
- */
- public static function getTimeZone() {
- // $userTz = JFactory::getUser()->getParam('timezone');
- // $timeZone = JFactory::getConfig()->getValue('offset');
- // if($userTz) {
- // $timeZone = $userTz;
- // }
- $config = JFactory::getConfig();
- $jtz = $config->get('offset');
- return new DateTimeZone($jtz);
- }
- /**
- * Method to get date for next month one month minus one day from $monthDay
- *
- * @param int $monthDay The day to start calculating the new date from
- * @param int $month The month datepart of the $monthDay
- * @param int $year The year the $monthDay is in
- *
- * @return JDate Containing the new date.
- *
- * @throws InvalidArgumentException
- */
- public static function getNextMonthDate($monthDay, $month, $year, $endOfDay=TRUE)
- {
- if (!is_numeric($monthDay)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_DAY_INT_INVALID'), __METHOD__);
- }
- if (($monthDay < 1) || ($monthDay > 28)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_DAY_RANGE_INVALID'), __METHOD__);
- }
- if (!is_numeric($month)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_INT_INVALID'), __METHOD__);
- }
- if (($month < 1) || ($month > 12)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_RANGE_INVALID'), __METHOD__);
- }
- if (!is_numeric($year)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_YEAR_INT_INVALID'), __METHOD__);
- }
- $newMonth = (int)$month;
- $newYear = abs((int)$year);
- $startDate = new JDate("$newYear-$newMonth-$monthDay 00:00:00");
- // if we are at the beginning of the month then get the last day of the month
- // if not the first of the month then we will increase the month values
- if ($monthDay > 1) {
- if ($newMonth == 12) {
- $newMonth=1;
- $newYear += 1;
- } else {
- $newMonth += 1;
- }
- }
- $nextMonthDate = new JDate("$newYear-$newMonth-$monthDay 00:00:00");
- $strDatePrefix = '';
- if ($endOfDay === TRUE)
- {
- $strDatePrefix .= '23:59:59';
- } else {
- $strDatePrefix .= '00:00:00';
- }
- $retval = null;
- if ($monthDay == 1)
- {
- $sDate = $nextMonthDate->year . '-' . $nextMonthDate->month . '-' . $nextMonthDate->daysinmonth . ' ' . $strDatePrefix;
- $retval = new JDate($sDate);
- } else {
- $sDate = $nextMonthDate->year . '-' . $nextMonthDate->month . '-' . ($monthDay -1) . ' ' . $strDatePrefix;
- $retval = new JDate($sDate);
- }
- // set the timezone to UTC
- $tz = new DateTimeZone('GMT');
- $retval->setTimezone($tz);
- return $retval;
- }
- /**
- * Method to get build date for given parameters
- *
- * @param int $monthDay The day to start calculating the new date from
- * @param int $month The month datepart of the $monthDay
- * @param int $year The year the $monthDay is in
- * @param mixed $tzOffset The timezone offset.
- *
- * @return JDate Containing the new date.
- *
- * @throws InvalidArgumentException
- */
- public static function getMonthStart($monthDay, $month, $year, $tzOffset=null)
- {
- if (!is_numeric($monthDay)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_DAY_INT_INVALID'), __METHOD__);
- }
- if (($monthDay < 1) || ($monthDay > 28)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_DAY_RANGE_INVALID'), __METHOD__);
- }
- if (!is_numeric($month)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_INT_INVALID'), __METHOD__);
- }
- if (($month < 1) || ($month > 12)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_RANGE_INVALID'), __METHOD__);
- }
- if (!is_numeric($year)) {
- throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_YEAR_INT_INVALID'), __METHOD__);
- }
- $newMonth = (int)$month;
- $newYear = abs((int)$year);
- $mydate = JFactory::getDate();
- if (is_null($tzOffset))
- {
- // JFactory::getApplication()->enqueueMessage(__METHOD__ . " Timezone null");
- $retval = new JDate("$newYear-$newMonth-$monthDay 00:00:00");
- } else {
- // JFactory::getApplication()->enqueueMessage(__METHOD__ . " Not Null : $tzOffset");
- $retval = new JDate("$newYear-$newMonth-$monthDay", $tzOffset);
- // JFactory::getApplication()->enqueueMessage(__METHOD__ . " Not Null : " . $retval->toSql());
- }
- // $retval = (is_null($tzOffset) ? new JDate("$newYear-$newMonth-$monthDay 00:00:00") : new JDate("$newYear-$newMonth-$monthDay", $tzOffset));
- // Now change the timezone to UTC.
- $tz = new DateTimeZone('GMT');
- $retval->setTimezone($tz);
- return $retval;
- }
- /**
- * Calculates the difference between two dates in months
- *
- * @param string $date1 The first date as a date string
- * @param string $date2 The second date as a date string
- * @param boolean $truncate If false an extra month wil be added
- * if extra day(s) exist
- *
- * @return int the number of months between the two dates.
- */
- public static function monthsBetweenDates($date1, $date2, $truncate=true)
- {
- $start = new DateTime($date1);
- $end = new DateTime($date2);
- $interval = $start->diff($end);
- $totalMonths = (($interval->y * 12) + $interval->m);
- if (($truncate == false) && ($interval->d >= 1))
- {
- $totalMonths++;
- }
- return $totalMonths;
- }
- /**
- * Calculates the difference between two dates in months and
- * returns a DatePeriod with the values
- *
- * @param string $date1 The first date as a date string
- * @param string $date2 The second date as a date string
- * @param boolean $truncate If false an extra month wil be added
- * if extra day(s) exist
- *
- * @return DatePeriod An object containing the dates between two dates
- */
- public static function getMonthDateArray($date1, $date2, $truncate=true)
- {
- $recurrences = cnreportDateHelper::monthsBetweenDates($date1, $date2, $truncate);
- $start = new DateTime($date1);
- // $end = new DateTime($date2);
- $interval = new DateInterval('P1M'); // 1 month intervals for 1 day use P1D for every seven days P7D
- $period = new DatePeriod($start, $interval, $recurrences);
- return $period;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement