/app/code/local/AW/Advancedreports/Helper/Date.php

https://bitbucket.org/spenna/alexoo_produzione · PHP · 225 lines · 127 code · 47 blank · 51 comment · 20 complexity · 5e219e988cd3ebcc88cb24b9504731fc MD5 · raw file

  1. <?php
  2. /**
  3. * aheadWorks Co.
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the EULA
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
  11. *
  12. * =================================================================
  13. * MAGENTO EDITION USAGE NOTICE
  14. * =================================================================
  15. * This package designed for Magento COMMUNITY edition
  16. * aheadWorks does not guarantee correct work of this extension
  17. * on any other Magento edition except Magento COMMUNITY edition.
  18. * aheadWorks does not provide extension support in case of
  19. * incorrect edition usage.
  20. * =================================================================
  21. *
  22. * @category AW
  23. * @package AW_Advancedreports
  24. * @copyright Copyright (c) 2010-2011 aheadWorks Co. (http://www.aheadworks.com)
  25. * @license http://ecommerce.aheadworks.com/AW-LICENSE-COMMUNITY.txt
  26. */
  27. class AW_Advancedreports_Helper_Date extends AW_Advancedreports_Helper_Data
  28. {
  29. /**
  30. * Retrives formatted datetime
  31. * Implements standart strptime() for crossplatformed use
  32. * @param Datetime $sDate
  33. * @param string $sFormat
  34. * @return string
  35. */
  36. public function strptime($sDate, $sFormat)
  37. {
  38. $aResult = array
  39. (
  40. 'tm_sec' => 0,
  41. 'tm_min' => 0,
  42. 'tm_hour' => 0,
  43. 'tm_mday' => 1,
  44. 'tm_mon' => 0,
  45. 'tm_year' => 0,
  46. 'tm_wday' => 0,
  47. 'tm_yday' => 0,
  48. 'unparsed' => $sDate,
  49. );
  50. while($sFormat != "")
  51. {
  52. // ===== Search a %x element, Check the static string before the %x =====
  53. $nIdxFound = strpos($sFormat, '%');
  54. if($nIdxFound === false)
  55. {
  56. // There is no more format. Check the last static string.
  57. $aResult['unparsed'] = ($sFormat == $sDate) ? "" : $sDate;
  58. break;
  59. }
  60. $sFormatBefore = substr($sFormat, 0, $nIdxFound);
  61. $sDateBefore = substr($sDate, 0, $nIdxFound);
  62. if($sFormatBefore != $sDateBefore) break;
  63. // ===== Read the value of the %x found =====
  64. $sFormat = substr($sFormat, $nIdxFound);
  65. $sDate = substr($sDate, $nIdxFound);
  66. $aResult['unparsed'] = $sDate;
  67. $sFormatCurrent = substr($sFormat, 0, 2);
  68. $sFormatAfter = substr($sFormat, 2);
  69. $nValue = -1;
  70. $sDateAfter = "";
  71. switch($sFormatCurrent)
  72. {
  73. case '%S': // Seconds after the minute (0-59)
  74. sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
  75. if(($nValue < 0) || ($nValue > 59)) return false;
  76. $aResult['tm_sec'] = $nValue;
  77. break;
  78. // ----------
  79. case '%M': // Minutes after the hour (0-59)
  80. sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
  81. if(($nValue < 0) || ($nValue > 59)) return false;
  82. $aResult['tm_min'] = $nValue;
  83. break;
  84. // ----------
  85. case '%H': // Hour since midnight (0-23)
  86. sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
  87. if(($nValue < 0) || ($nValue > 23)) return false;
  88. $aResult['tm_hour'] = $nValue;
  89. break;
  90. // ----------
  91. case '%e':
  92. case '%d': // Day of the month (1-31)
  93. sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
  94. if(($nValue < 1) || ($nValue > 31)) return false;
  95. $aResult['tm_mday'] = $nValue;
  96. break;
  97. // ----------
  98. case '%m': // Months since January (0-11)
  99. sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
  100. if(($nValue < 1) || ($nValue > 12)) return false;
  101. $aResult['tm_mon'] = ($nValue - 1);
  102. break;
  103. // ----------
  104. case '%y': // Years since 1900
  105. sscanf($sDate, "%2d%[^\\n]", $nValue, $sDateAfter);
  106. if($nValue >= 69 && $nValue <= 99){
  107. $aResult['tm_year'] = $nValue;
  108. } else {
  109. $aResult['tm_year'] = ($nValue + 100);
  110. }
  111. break;
  112. // ----------
  113. case '%Y': // Years since 1900
  114. sscanf($sDate, "%4d%[^\\n]", $nValue, $sDateAfter);
  115. if($nValue < 1900) return false;
  116. $aResult['tm_year'] = ($nValue - 1900);
  117. break;
  118. // ----------
  119. default:
  120. break 2; // Break Switch and while
  121. } // END of case format
  122. // ===== Next please =====
  123. $sFormat = $sFormatAfter;
  124. $sDate = $sDateAfter;
  125. $aResult['unparsed'] = $sDate;
  126. } // END of while($sFormat != "")
  127. // ===== Create the other value of the result array =====
  128. $nParsedDateTimestamp = mktime($aResult['tm_hour'], $aResult['tm_min'], $aResult['tm_sec'],
  129. $aResult['tm_mon'] + 1, $aResult['tm_mday'], $aResult['tm_year'] + 1900);
  130. // Before PHP 5.1 return -1 when error
  131. if(($nParsedDateTimestamp === false)
  132. ||($nParsedDateTimestamp === -1)) return false;
  133. $aResult['tm_wday'] = (int) strftime("%w", $nParsedDateTimestamp); // Days since Sunday (0-6)
  134. $aResult['tm_yday'] = (strftime("%j", $nParsedDateTimestamp) - 1); // Days since January 1 (0-365)
  135. return $aResult;
  136. }
  137. public function incSec($datetime)
  138. {
  139. $date = new Zend_Date(
  140. $datetime,
  141. self::MYSQL_ZEND_DATE_FORMAT,
  142. $this->getLocale()->getLocaleCode()
  143. );
  144. $date->addSecond(1);
  145. return $date->toString(self::MYSQL_ZEND_DATE_FORMAT);
  146. }
  147. public function decSec($datetime)
  148. {
  149. $date = new Zend_Date(
  150. $datetime,
  151. self::MYSQL_ZEND_DATE_FORMAT,
  152. $this->getLocale()->getLocaleCode()
  153. );
  154. $date->subSecond(1);
  155. return $date->toString(self::MYSQL_ZEND_DATE_FORMAT);
  156. }
  157. /**
  158. * Retrives day period (timezone offset is included)
  159. *
  160. * @param <type> $datetime
  161. * @return <type>
  162. */
  163. public function getThisDayPeriod($datetime)
  164. {
  165. $date = new Zend_Date(
  166. $datetime,
  167. Zend_Date::ISO_8601
  168. );
  169. $dateFrom = clone $date;
  170. $dateFrom->setHour(0)->setMinute(0)->setSecond(0)->addSecond($this->getTimeZoneOffset());
  171. $dateTo = clone $date;
  172. $dateTo->setHour(23)->setMinute(59)->setSecond(59)->addSecond($this->getTimeZoneOffset());
  173. return array(
  174. 'from' => $dateFrom->toString(self::MYSQL_ZEND_DATE_FORMAT),
  175. 'to' => $dateTo->toString(self::MYSQL_ZEND_DATE_FORMAT)
  176. );
  177. }
  178. }