You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

65 lines
1.6 KiB

  1. <?php
  2. /**
  3. * *************************************************************************
  4. *
  5. * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
  6. *
  7. * ************************************************************************
  8. */
  9. /**
  10. *
  11. * @file utils.php
  12. * @encoding UTF-8
  13. *
  14. *
  15. * @date 2014年12月25日
  16. *
  17. */
  18. class PushUtils {
  19. /**
  20. * get the timestamp at current dat start
  21. *
  22. * NOTE: the result start at the timezone (GTM +8) 00:00:00, not the GTM 0.
  23. *
  24. * @param
  25. * {number} offset, offset of timezone;
  26. * @return number
  27. */
  28. public static function getDayTimestamp($now = null, $offset = 8) {
  29. $now = $now === null ? time() : $now;
  30. $gtm0 = $now - ($now % 86400); // GTM 0
  31. $dayline = ($now - $gtm0) / 3600;
  32. // 时差跨天
  33. if ($dayline + $offset > 23) {
  34. return $gtm0 + 86400 + $offset * 3600 * - 1;
  35. } else {
  36. return $gtm0 + $offset * 3600 * - 1;
  37. }
  38. }
  39. /**
  40. * get the timestamp at current hour start
  41. * @param int $num 指定时间
  42. * @return number
  43. */
  44. public static function getHourTimestamp($now = null) {
  45. $now = $now === null ? time() : $now;
  46. $diff = $now % 3600; // 1 * 60 * 60;
  47. return $now - $diff;
  48. }
  49. /**
  50. * get the timestamp at current minute start
  51. * @param int $num 指定时间
  52. * @return number
  53. */
  54. public static function getMinuteTimestamp($now = null) {
  55. $now = $now === null ? time() : $now;
  56. $diff = $now % 60;
  57. return $now - $diff;
  58. }
  59. }