Advertisement
AmourSpirit

joomla date helper class

Aug 3rd, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.55 KB | None | 0 0
  1. <?php
  2.  
  3. use Joomla\Registry\Registry;
  4. use Psr\Log\InvalidArgumentException;
  5. use Joomla\String\String;
  6. /**
  7.  * @version     $id$
  8.  * @package     com_cn_reports
  9.  * @copyright   Copyright (C) 2015. All rights reserved.
  10.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  11.  * @author    
  12.  */
  13. // No direct access
  14. defined('_JEXEC') or die;
  15.  
  16. /**
  17.  * Cn_reports helper.
  18.  */
  19. abstract class cnreportDateHelper
  20. {
  21.     /**
  22.      * Gets the date offset for timezone as UTC/GMT.
  23.      * If the timezone offset is a negative value then the return value is less than the
  24.      * UTC/GMT value.<br />Otherwise the return date is ahead to the UTC/GMT by the amount of the
  25.      * timezone offset.
  26.      * @param JDate $objDate the input date
  27.      * @return JData UTC/GMT date set by the offset of $objDate
  28.      * @throws InvalidArgumentException
  29.      * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
  30.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
  31.      * $displayDate = cnreportDateHelper::getDateTimeFromUtc($mydate);<br />
  32.      * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 11:00:00 PM displays as UTC + 10h<br />
  33.      * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
  34.      * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
  35.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 05:00:00 PM displays as UTC + 4h<br />
  36.      * $displayDate = cnreportDateHelper::getDateTimeFromUtc($mydate);<br />
  37.      * echo $displayDate->format('Y-m-d h:i:s A'); // 2015-08-02 09:00:00 AM PM displays as UTC - 4h<br />
  38.      */
  39.     public static function getDateTimeFromUtc($objDate=null)
  40.     {
  41.         if (!is_null($objDate)) {
  42.             if (is_object($objDate) && (!($objDate instanceof JDate)))
  43.             {
  44.                 throw new InvalidArgumentException('$objDate not set to an instace of JDate');
  45.             }
  46.             if (!is_object($objDate)) {
  47.                 $objDate = JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
  48.             }
  49.         } else {
  50.             $objDate = JFactory::getDate('now', cnreportDateHelper::getOffset());
  51.         }
  52.         $offSec =  $objDate->getOffsetFromGMT();
  53.         if ($offSec == 0) {
  54.             return clone $objDate;
  55.         }
  56.         // create a new date in UTC/GMT from $objDate using
  57.         // the UTC/GMT fo the $objDate
  58.         $retval = JFactory::getDate($objDate->format('Y-m-d H:i:s', true), 'UTC');
  59.  
  60.         $sint =  'PT' . abs($offSec) .'S'; // formated for time with seconds
  61.         $interval = new DateInterval($sint);
  62.         // if the timezone offset is negative then invert the DateInterval
  63.         // this will allow the time to be subtraced rather than added
  64.         if ($offSec < 0) {
  65.             $interval->invert =1;
  66.         }
  67.  
  68.         // add the interval
  69.         $retval->add($interval);
  70.         return $retval;
  71.     }
  72.  
  73.     /**
  74.      * Gets the Start of the day from the date passed in adjusted to the timezone of date.
  75.      * In other wordes set the date to 12:00:00 AM of the same day and the applies the timezone.<br />
  76.      * If the timezone offset is a negative value then the return value is less than the
  77.      * UTC/GMT value.<br />Otherwise the return date is ahead to the UTC/GMT by the amount of the
  78.      * timezone offset.
  79.      * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
  80.      * @throws InvalidArgumentException
  81.      * @return JDate UTC/GMT date set to the start of the day plus timezone offset
  82.      * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
  83.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
  84.      * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
  85.      * 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 />
  86.      * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
  87.      * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
  88.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC + 4h<br />
  89.      * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
  90.      * 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 />
  91.      */
  92.     public static function getDateStartFromUtc($objDate = null)
  93.     {
  94.         $retval = null;
  95.         if (!is_null($objDate)) {
  96.             if (is_object($objDate) && (!($objDate instanceof JDate)))
  97.             {
  98.                 throw new InvalidArgumentException('$objDate not set to an instace of JDate');
  99.             }
  100.             $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
  101.  
  102.         } else {
  103.             $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
  104.         }
  105.         $retval->setTime(0, 0, 0);
  106.         $retval = cnreportDateHelper::getDateTimeFromUtc($retval);
  107.  
  108.         return $retval;
  109.     }
  110.  
  111.     /**
  112.      * Gets the End of the day from the date passed in adjusted to the timezone of date.
  113.      * In other wordes set the date to 11:59:59 PM of the same day and the applies the timezone.<br />
  114.      * If the timezone offset is a negative value then the return value is less than the
  115.      * UTC/GMT value.<br />Otherwise the return date is ahead to the UTC/GMT by the amount of the
  116.      * timezone offset.
  117.      * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
  118.      * @throws InvalidArgumentException
  119.      * @return JDate UTC/GMT date set to the start of the day plus timezone offset
  120.      * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
  121.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
  122.      * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
  123.      * 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 />
  124.      * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
  125.      * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
  126.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC + 4h<br />
  127.      * $displayDate = cnreportDateHelper::getDateStartFromUtc($mydate);<br />
  128.      * 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 />
  129.      */
  130.     public static function getDateEndFromUtc($objDate = null)
  131.     {
  132.         $retval = null;
  133.         if (!is_null($objDate)) {
  134.             if (is_object($objDate) && (!($objDate instanceof JDate)))
  135.             {
  136.                 throw new InvalidArgumentException('$objDate not set to an instace of JDate');
  137.             }
  138.             $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
  139.  
  140.         } else {
  141.             $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
  142.         }
  143.         $retval->setTime(23, 59, 59);
  144.         $retval = cnreportDateHelper::getDateTimeFromUtc($retval);
  145.  
  146.         return $retval;
  147.     }
  148.  
  149.     /**
  150.      * Gets the Start of the day from the date passed in adjusted to the timezone of date.
  151.      * In other wordes set the date to 12:00:00 AM of the same day and the applies the timezone.<br />
  152.      * If the timezone offset is a negative value then the return value is greater than the
  153.      * UTC/GMT value.<br />Otherwise the return date is behind to the UTC/GMT by the amount of the
  154.      * timezone offset.<br />
  155.      * This is the same format used by Joomla. By Default Negative Timezone offsets are added to the time and a positive
  156.      * and Postitive Timezone offsets are subtracted from the time.
  157.      * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
  158.      * @throws InvalidArgumentException
  159.      * @return JDate UTC/GMT date set to the start of the day plus timezone offset
  160.      * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
  161.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
  162.      * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
  163.      * 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 />
  164.      * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
  165.      * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
  166.      * echo $mydate->format('Y-m-d h:i:s A', false); //2015-08-02 03:00:00 AM displays as UTC + 4h<br />
  167.      * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
  168.      * 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 />
  169.      */
  170.     public static function getDateStart($objDate = null)
  171.     {
  172.         $retval = null;
  173.         if (!is_null($objDate)) {
  174.             if (is_object($objDate) && (!($objDate instanceof JDate)))
  175.             {
  176.                 throw new InvalidArgumentException('$objDate not set to an instace of JDate');
  177.             }
  178.             $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
  179.  
  180.         } else {
  181.             $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
  182.         }
  183.         $retval->setTime(0, 0, 0);
  184.         // Now change the timezone back to UTC.
  185.         $tz = new DateTimeZone('GMT');
  186.         $retval->setTimezone($tz);
  187.         return $retval;
  188.     }
  189.  
  190.     /**
  191.      * Gets the End of the day from the date passed in adjusted to the timezone of date.
  192.      * In other wordes set the date to 12:00:00 AM of the same day and the applies the timezone.<br />
  193.      * If the timezone offset is a negative value then the return value is greater than the
  194.      * UTC/GMT value.<br />Otherwise the return date is behind to the UTC/GMT by the amount of the
  195.      * timezone offset.<br />
  196.      * This is the same format used by Joomla. By Default Negative Timezone offsets are added to the time and a positive
  197.      * and Postitive Timezone offsets are subtracted from the time.
  198.      * @param string $objDate Optional JDate Object or String representing date. If null defults to current Joomla server time.
  199.      * @throws InvalidArgumentException
  200.      * @return JDate UTC/GMT date set to the start of the day plus timezone offset
  201.      * @example $mydate = JFactory::getDate('2015-08-02 13:00:00','Australia/Victoria'); // victoria is +10h from UTC<br />
  202.      * echo $mydate->format('Y-m-d h:i:s A', false); // 2015-08-02 03:00:00 AM displays as UTC - 10h<br />
  203.      * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
  204.      * 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 />
  205.      * echo $displayDate->getOffsetFromGMT(); // 0 function returns date as UTC/GMT<br /><br />
  206.      * $mydate = JFactory::getDate('2015-08-02 13:00:00','America/Toronto'); // Toronto is -4 or -5 depending on DST from UTC<br />
  207.      * echo $mydate->format('Y-m-d h:i:s A', false); //2015-08-02 03:00:00 AM displays as UTC + 4h<br />
  208.      * $displayDate = cnreportDateHelper::getDateStart($mydate);<br />
  209.      * 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 />
  210.      */
  211.     public static function getDateEnd($objDate = null)
  212.     {
  213.         $retval = null;
  214.         if (!is_null($objDate)) {
  215.             if (is_object($objDate) && (!($objDate instanceof JDate)))
  216.             {
  217.                 throw new InvalidArgumentException('$objDate not set to an instace of JDate');
  218.             }
  219.             $retval = is_object($objDate) ? clone $objDate: JFactory::getDate((string) $objDate, cnreportDateHelper::getOffset());
  220.  
  221.         } else {
  222.             $retval = JFactory::getDate('now', cnreportDateHelper::getOffset());
  223.         }
  224.         $retval->setTime(23, 59, 59);
  225.         $tz = new DateTimeZone('GMT');
  226.         $retval->setTimezone($tz);
  227.         return $retval;
  228.     }
  229.  
  230.     /**
  231.      * Helper method to get Joomlas Timezone
  232.      * @return String contain timezone such sucha as 'America/Toronto'
  233.      */
  234.     public static function getOffset() {
  235.         $config = JFactory::getConfig();
  236.         $jtz = $config->get('offset');
  237.         return $jtz;
  238.     }
  239.  
  240.     /**
  241.      * Returns the userTime zone if the user has set one, or the global config one
  242.      * @return DateTimeZone
  243.      */
  244.     public static function getTimeZoneUser() {
  245.         $user = JFactory::getUser();
  246.         $userTz = $user->getParam('timezone');
  247.  
  248.         $config = JFactory::getConfig();
  249.         $timeZone = $config->get('offset');
  250.         if($userTz) {
  251.             $timeZone = $userTz;
  252.         }
  253.         return new DateTimeZone($timeZone);
  254.     }
  255.  
  256.     /**
  257.      * Returns the the global timezone
  258.      * @return DateTimeZone
  259.      */
  260.     public static function getTimeZone() {
  261.         //      $userTz = JFactory::getUser()->getParam('timezone');
  262.         //      $timeZone = JFactory::getConfig()->getValue('offset');
  263.         //      if($userTz) {
  264.         //          $timeZone = $userTz;
  265.         //      }
  266.         $config = JFactory::getConfig();
  267.         $jtz = $config->get('offset');
  268.  
  269.  
  270.         return new DateTimeZone($jtz);
  271.     }
  272.  
  273.     /**
  274.      * Method to get date for next month one month minus one day from $monthDay
  275.      *
  276.      * @param int $monthDay The day to start calculating the new date from
  277.      * @param int $month The month datepart of the $monthDay
  278.      * @param int $year The year the $monthDay is in
  279.      *
  280.      * @return JDate Containing the new date.
  281.      *
  282.      * @throws InvalidArgumentException
  283.      */
  284.     public static function getNextMonthDate($monthDay, $month, $year, $endOfDay=TRUE)
  285.     {
  286.  
  287.         if (!is_numeric($monthDay)) {
  288.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_DAY_INT_INVALID'), __METHOD__);
  289.         }
  290.         if (($monthDay < 1) || ($monthDay > 28)) {
  291.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_DAY_RANGE_INVALID'), __METHOD__);
  292.         }
  293.         if (!is_numeric($month)) {
  294.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_INT_INVALID'), __METHOD__);
  295.         }
  296.         if (($month < 1) || ($month > 12)) {
  297.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_RANGE_INVALID'), __METHOD__);
  298.         }
  299.         if (!is_numeric($year)) {
  300.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_YEAR_INT_INVALID'), __METHOD__);
  301.         }
  302.  
  303.         $newMonth = (int)$month;
  304.         $newYear = abs((int)$year);
  305.  
  306.  
  307.         $startDate = new JDate("$newYear-$newMonth-$monthDay 00:00:00");
  308.         // if we are at the beginning of the month then get the last day of the month
  309.         // if not the first of the month then we will increase the month values
  310.         if ($monthDay > 1) {
  311.             if ($newMonth == 12) {
  312.                 $newMonth=1;
  313.                 $newYear += 1;
  314.             } else {
  315.                 $newMonth += 1;
  316.             }
  317.         }
  318.  
  319.         $nextMonthDate = new JDate("$newYear-$newMonth-$monthDay 00:00:00");
  320.  
  321.         $strDatePrefix = '';
  322.         if ($endOfDay === TRUE)
  323.         {
  324.             $strDatePrefix .= '23:59:59';
  325.         } else {
  326.             $strDatePrefix .= '00:00:00';
  327.         }
  328.  
  329.         $retval = null;
  330.         if ($monthDay == 1)
  331.         {
  332.             $sDate = $nextMonthDate->year . '-' . $nextMonthDate->month . '-' . $nextMonthDate->daysinmonth . ' ' . $strDatePrefix;
  333.             $retval = new JDate($sDate);
  334.         } else {
  335.             $sDate = $nextMonthDate->year . '-' . $nextMonthDate->month . '-' . ($monthDay -1) . ' ' . $strDatePrefix;
  336.             $retval = new JDate($sDate);
  337.         }
  338.         // set the timezone to UTC
  339.         $tz = new DateTimeZone('GMT');
  340.         $retval->setTimezone($tz);
  341.         return $retval;
  342.     }
  343.  
  344.     /**
  345.      * Method to get build date for given parameters
  346.      *
  347.      * @param int $monthDay The day to start calculating the new date from
  348.      * @param int $month The month datepart of the $monthDay
  349.      * @param int $year The year the $monthDay is in
  350.      * @param   mixed  $tzOffset  The timezone offset.
  351.      *
  352.      * @return JDate Containing the new date.
  353.      *
  354.      * @throws InvalidArgumentException
  355.      */
  356.     public static function getMonthStart($monthDay, $month, $year, $tzOffset=null)
  357.     {
  358.  
  359.         if (!is_numeric($monthDay)) {
  360.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_DAY_INT_INVALID'), __METHOD__);
  361.         }
  362.         if (($monthDay < 1) || ($monthDay > 28)) {
  363.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_DAY_RANGE_INVALID'), __METHOD__);
  364.         }
  365.         if (!is_numeric($month)) {
  366.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_INT_INVALID'), __METHOD__);
  367.         }
  368.         if (($month < 1) || ($month > 12)) {
  369.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_MONTH_RANGE_INVALID'), __METHOD__);
  370.         }
  371.         if (!is_numeric($year)) {
  372.             throw new InvalidArgumentException(JText::_('COM_CN_REPORTS_ERR_YEAR_INT_INVALID'), __METHOD__);
  373.         }
  374.  
  375.         $newMonth = (int)$month;
  376.         $newYear = abs((int)$year);
  377.         $mydate = JFactory::getDate();
  378.         if (is_null($tzOffset))
  379.         {
  380.             //          JFactory::getApplication()->enqueueMessage(__METHOD__ . " Timezone null");
  381.             $retval = new JDate("$newYear-$newMonth-$monthDay 00:00:00");
  382.         } else {
  383.             //          JFactory::getApplication()->enqueueMessage(__METHOD__ . " Not Null : $tzOffset");
  384.             $retval = new JDate("$newYear-$newMonth-$monthDay", $tzOffset);
  385.             //          JFactory::getApplication()->enqueueMessage(__METHOD__ . " Not Null : " . $retval->toSql());
  386.         }
  387.         // $retval = (is_null($tzOffset) ? new JDate("$newYear-$newMonth-$monthDay 00:00:00") : new JDate("$newYear-$newMonth-$monthDay", $tzOffset));
  388.  
  389.         // Now change the timezone to UTC.
  390.         $tz = new DateTimeZone('GMT');
  391.         $retval->setTimezone($tz);
  392.         return $retval;
  393.     }
  394.  
  395.     /**
  396.      * Calculates the difference between two dates in months
  397.      *
  398.      * @param string $date1 The first date as a date string
  399.      * @param string $date2 The second date as a date string
  400.      * @param boolean $truncate If false an extra month wil be added
  401.      * if extra day(s) exist
  402.      *
  403.      * @return int the number of months between the two dates.
  404.      */
  405.     public static function monthsBetweenDates($date1, $date2, $truncate=true)
  406.     {
  407.         $start = new DateTime($date1);
  408.         $end = new DateTime($date2);
  409.         $interval = $start->diff($end);
  410.         $totalMonths = (($interval->y * 12) + $interval->m);
  411.         if (($truncate == false) && ($interval->d >= 1))
  412.         {
  413.             $totalMonths++;
  414.         }
  415.         return $totalMonths;
  416.     }
  417.  
  418.     /**
  419.      * Calculates the difference between two dates in months and
  420.      * returns a DatePeriod with the values
  421.      *
  422.      * @param string $date1 The first date as a date string
  423.      * @param string $date2 The second date as a date string
  424.      * @param boolean $truncate If false an extra month wil be added
  425.      * if extra day(s) exist
  426.      *
  427.      * @return DatePeriod An object containing the dates between two dates
  428.      */
  429.     public static function getMonthDateArray($date1, $date2, $truncate=true)
  430.     {
  431.  
  432.         $recurrences = cnreportDateHelper::monthsBetweenDates($date1, $date2, $truncate);
  433.         $start = new DateTime($date1);
  434.         // $end = new DateTime($date2);
  435.         $interval = new DateInterval('P1M'); // 1 month intervals for 1 day use P1D for every seven days P7D
  436.  
  437.         $period = new DatePeriod($start, $interval, $recurrences);
  438.         return $period;
  439.     }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement