/osCommerce/OM/Core/DateTime.php

https://github.com/shooray/oscommerce · PHP · 283 lines · 181 code · 58 blank · 44 comment · 72 complexity · 76ce9fb0730c6125413818244159c9c0 MD5 · raw file

  1. <?php
  2. /**
  3. * osCommerce Online Merchant
  4. *
  5. * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
  6. * @license BSD License; http://www.oscommerce.com/bsdlicense.txt
  7. */
  8. namespace osCommerce\OM\Core;
  9. class DateTime {
  10. /**
  11. * @since v3.0.0
  12. */
  13. const DEFAULT_FORMAT = 'Y-m-d H:i:s';
  14. /**
  15. * @since v3.0.0
  16. */
  17. public static function getNow($format = null) {
  18. if ( !isset($format) ) {
  19. $format = self::DEFAULT_FORMAT;
  20. }
  21. return date($format);
  22. }
  23. /**
  24. * @since v3.0.0
  25. */
  26. public static function getShort($date = null, $with_time = false) {
  27. $OSCOM_Language = Registry::get('Language');
  28. if ( !isset($date) ) {
  29. $date = self::getNow();
  30. }
  31. $year = substr($date, 0, 4);
  32. $month = (int)substr($date, 5, 2);
  33. $day = (int)substr($date, 8, 2);
  34. $hour = (int)substr($date, 11, 2);
  35. $minute = (int)substr($date, 14, 2);
  36. $second = (int)substr($date, 17, 2);
  37. if ( @date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year ) {
  38. return strftime($OSCOM_Language->getDateFormatShort($with_time), mktime($hour, $minute, $second, $month, $day, $year));
  39. } else {
  40. return preg_replace('/2037/', $year, strftime($OSCOM_Language->getDateFormatShort($with_time), mktime($hour, $minute, $second, $month, $day, 2037)));
  41. }
  42. }
  43. /**
  44. * @since v3.0.0
  45. */
  46. public static function getLong($date = null) {
  47. $OSCOM_Language = Registry::get('Language');
  48. if ( !isset($date) ) {
  49. $date = self::getNow();
  50. }
  51. $year = substr($date, 0, 4);
  52. $month = (int)substr($date, 5, 2);
  53. $day = (int)substr($date, 8, 2);
  54. $hour = (int)substr($date, 11, 2);
  55. $minute = (int)substr($date, 14, 2);
  56. $second = (int)substr($date, 17, 2);
  57. if ( @date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year ) {
  58. return strftime($OSCOM_Language->getDateFormatLong(), mktime($hour, $minute, $second, $month, $day, $year));
  59. } else {
  60. return preg_replace('/2037/', $year, strftime($OSCOM_Language->getDateFormatLong(), mktime($hour, $minute, $second, $month, $day, 2037)));
  61. }
  62. }
  63. /**
  64. * @since v3.0.0
  65. */
  66. public static function getTimestamp($date = null, $format = null) {
  67. if ( !isset($date) ) {
  68. $date = self::getNow($format);
  69. }
  70. if ( !isset($format) ) {
  71. $format = self::DEFAULT_FORMAT;
  72. }
  73. $dt = \DateTime::createFromFormat($format, $date);
  74. $timestamp = $dt->getTimestamp();
  75. return $timestamp;
  76. }
  77. /**
  78. * @since v3.0.0
  79. */
  80. public static function fromUnixTimestamp($timestamp, $format = null) {
  81. if ( !isset($format) ) {
  82. $format = self::DEFAULT_FORMAT;
  83. }
  84. return date($format, $timestamp);
  85. }
  86. /**
  87. * @since v3.0.0
  88. */
  89. public static function isLeapYear($year = null) {
  90. if ( !isset($year) ) {
  91. $year = self::getNow('Y');
  92. }
  93. if ( $year % 100 == 0 ) {
  94. if ( $year % 400 == 0 ) {
  95. return true;
  96. }
  97. } else {
  98. if ( ($year % 4) == 0 ) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * @since v3.0.0
  106. */
  107. public static function validate($date_to_check, $format_string, &$date_array) {
  108. $separator_idx = -1;
  109. $separators = array('-', ' ', '/', '.');
  110. $month_abbr = array('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
  111. $no_of_days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  112. $format_string = strtolower($format_string);
  113. if (strlen($date_to_check) != strlen($format_string)) {
  114. return false;
  115. }
  116. $size = sizeof($separators);
  117. for ($i=0; $i<$size; $i++) {
  118. $pos_separator = strpos($date_to_check, $separators[$i]);
  119. if ($pos_separator != false) {
  120. $date_separator_idx = $i;
  121. break;
  122. }
  123. }
  124. for ($i=0; $i<$size; $i++) {
  125. $pos_separator = strpos($format_string, $separators[$i]);
  126. if ($pos_separator != false) {
  127. $format_separator_idx = $i;
  128. break;
  129. }
  130. }
  131. if ($date_separator_idx != $format_separator_idx) {
  132. return false;
  133. }
  134. if ($date_separator_idx != -1) {
  135. $format_string_array = explode( $separators[$date_separator_idx], $format_string );
  136. if (sizeof($format_string_array) != 3) {
  137. return false;
  138. }
  139. $date_to_check_array = explode( $separators[$date_separator_idx], $date_to_check );
  140. if (sizeof($date_to_check_array) != 3) {
  141. return false;
  142. }
  143. $size = sizeof($format_string_array);
  144. for ($i=0; $i<$size; $i++) {
  145. if ($format_string_array[$i] == 'mm' || $format_string_array[$i] == 'mmm') $month = $date_to_check_array[$i];
  146. if ($format_string_array[$i] == 'dd') $day = $date_to_check_array[$i];
  147. if ( ($format_string_array[$i] == 'yyyy') || ($format_string_array[$i] == 'aaaa') ) $year = $date_to_check_array[$i];
  148. }
  149. } else {
  150. if (strlen($format_string) == 8 || strlen($format_string) == 9) {
  151. $pos_month = strpos($format_string, 'mmm');
  152. if ($pos_month != false) {
  153. $month = substr( $date_to_check, $pos_month, 3 );
  154. $size = sizeof($month_abbr);
  155. for ($i=0; $i<$size; $i++) {
  156. if ($month == $month_abbr[$i]) {
  157. $month = $i;
  158. break;
  159. }
  160. }
  161. } else {
  162. $month = substr($date_to_check, strpos($format_string, 'mm'), 2);
  163. }
  164. } else {
  165. return false;
  166. }
  167. $day = substr($date_to_check, strpos($format_string, 'dd'), 2);
  168. $year = substr($date_to_check, strpos($format_string, 'yyyy'), 4);
  169. }
  170. if (strlen($year) != 4) {
  171. return false;
  172. }
  173. if (!settype($year, 'integer') || !settype($month, 'integer') || !settype($day, 'integer')) {
  174. return false;
  175. }
  176. if ($month > 12 || $month < 1) {
  177. return false;
  178. }
  179. if ($day < 1) {
  180. return false;
  181. }
  182. if ( self::isLeapYear($year) ) {
  183. $no_of_days[1] = 29;
  184. }
  185. if ($day > $no_of_days[$month - 1]) {
  186. return false;
  187. }
  188. $date_array = array($year, $month, $day);
  189. return true;
  190. }
  191. /**
  192. * Set the time zone to use for dates.
  193. *
  194. * @param string $time_zone An optional time zone to set to
  195. * @param string $site The Site to retrieve the time zone from
  196. * @return boolean
  197. * @since v3.0.1
  198. */
  199. public static function setTimeZone($time_zone = null, $site = 'OSCOM') {
  200. if ( !isset($time_zone) ) {
  201. if ( OSCOM::configExists('time_zone', $site) ) {
  202. $time_zone = OSCOM::getConfig('time_zone', $site);
  203. } else {
  204. $time_zone = date_default_timezone_get();
  205. }
  206. }
  207. return date_default_timezone_set($time_zone);
  208. }
  209. /**
  210. * Return an array of available time zones.
  211. *
  212. * @return array
  213. * @since v3.0.1
  214. */
  215. public static function getTimeZones() {
  216. $result = array();
  217. foreach ( \DateTimeZone::listIdentifiers() as $id ) {
  218. $tz_string = str_replace('_', ' ', $id);
  219. $id_array = explode('/', $tz_string, 2);
  220. $result[$id_array[0]][$id] = isset($id_array[1]) ? $id_array[1] : $id_array[0];
  221. }
  222. return $result;
  223. }
  224. }
  225. ?>