VsDateUtil.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /** @package verysimple::Util */
  3. /**
  4. * Static utility class for working with Dates
  5. *
  6. * @package verysimple::Util
  7. * @author Jason Hinkle
  8. * @copyright 1997-2011 VerySimple, Inc.
  9. * @license LGPL http://www.gnu.org/licenses/lgpl.html
  10. * @version 1.0
  11. */
  12. class VsDateUtil
  13. {
  14. /** @var int one day in milliseconds */
  15. static $ONE_DAY = 86400000;
  16. /** @var int one hour in milliseconds */
  17. static $ONE_HOUR = 3600000;
  18. /** @var int one minute in milliseconds */
  19. static $ONE_MINUTE = 6000;
  20. /** @var int one second in milliseconds */
  21. static $ONE_SECOND = 1000;
  22. /**
  23. * Return current date as string in the specified format
  24. * @param string $format
  25. */
  26. static function Today($format = "Y-m-d")
  27. {
  28. return self::Now($format);
  29. }
  30. /**
  31. * Returns the timestamp for the beginning of DST for specified year
  32. * @param int the 4-digit year (if not provide then the current year is used)
  33. */
  34. static function DstStartNorthAmerica($year = null)
  35. {
  36. if (!$year) $year = date('Y');
  37. return strtotime('03/01/' . $year . ' second sunday');
  38. }
  39. /**
  40. * Returns the timestamp for the end of DST for the specified year
  41. * @param int the 4-digit year (if not provide then the current year is used)
  42. */
  43. static function DstEndNorthAmerica($year = null)
  44. {
  45. if (!$year) $year = date('Y');
  46. return strtotime('11/01/' . $year . ' first sunday');
  47. }
  48. /**
  49. * Return true if the date is within the DST observation range for North America
  50. * @param int $timestamp (if not provided then the current server time will be used)
  51. */
  52. static function IsDstNorthAmerica($timestamp = null)
  53. {
  54. if (!$timestamp) $timestamp = time();
  55. return $timestamp > self::DstStartNorthAmerica() && $timestamp < self::DstEndNorthAmerica();
  56. }
  57. /**
  58. * Return current date/time as string in the specified format
  59. * @param string $format
  60. */
  61. static function Now($format = "Y-m-d H:i:s")
  62. {
  63. return date($format);
  64. }
  65. /**
  66. * Return yesterday's date as string in the specified format
  67. * @param string $format
  68. */
  69. static function Yesterday($format = "Y-m-d")
  70. {
  71. return self::DaysAgo(1,$format);
  72. }
  73. /**
  74. * Return tomorrow's date as string in the specified format
  75. * @param string $format
  76. */
  77. static function Tomorrow($format = "Y-m-d")
  78. {
  79. return self::DaysFromNow(1,$format);
  80. }
  81. /**
  82. * Return the date/time 24 hours ago as string in the specified format
  83. * @param string $format
  84. */
  85. static function TwentyFourHoursAgo($format = "Y-m-d H:i:s")
  86. {
  87. return self::HoursAgo(24,$format);
  88. }
  89. /**
  90. * Return the date/time 24 hours from now as string in the specified format
  91. * @param string $format
  92. */
  93. static function TwentyFourHoursFromNow($format = "Y-m-d H:i:s")
  94. {
  95. return self::HoursFromNow(24,$format);
  96. }
  97. /**
  98. * Return date as a string the specified number of days ago
  99. * @param int $days
  100. * @param string $format
  101. */
  102. static function DaysAgo($days, $format = "Y-m-d")
  103. {
  104. return date($format,strtotime(self::Now() . " - $days days"));
  105. }
  106. /**
  107. * Return date as a string the specified number of days from now
  108. * @param int $days
  109. * @param string $format
  110. */
  111. static function DaysFromNow($days, $format = "Y-m-d")
  112. {
  113. return date($format,strtotime(self::Now() . " + $days days"));
  114. }
  115. /**
  116. * Return date/time as a string the specified number of hours ago
  117. * @param int $hours
  118. * @param string $format
  119. */
  120. static function HoursAgo($hours, $format = "Y-m-d H:i:s")
  121. {
  122. return date($format,strtotime(self::Now() . " - $hours hours"));
  123. }
  124. /**
  125. * Return date/time as a string the specified number of hours from now
  126. * @param int $hours
  127. * @param string $format
  128. */
  129. static function HoursFromNow($hours, $format = "Y-m-d H:i:s")
  130. {
  131. return date($format,strtotime(self::Now() . " - $hours hours"));
  132. }
  133. }
  134. ?>