Açıklama Yok

DateTime.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace MailPoet\WP;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class DateTime {
  6. const DEFAULT_DATE_FORMAT = 'Y-m-d';
  7. const DEFAULT_TIME_FORMAT = 'H:i:s';
  8. const DEFAULT_DATE_TIME_FORMAT = 'Y-m-d H:i:s';
  9. /** @var WPFunctions */
  10. private $wp;
  11. public function __construct(
  12. WPFunctions $wp = null
  13. ) {
  14. if ($wp === null) {
  15. $wp = new WPFunctions();
  16. }
  17. $this->wp = $wp;
  18. }
  19. public function getTimeFormat() {
  20. $timeFormat = $this->wp->getOption('time_format');
  21. if (empty($timeFormat)) $timeFormat = self::DEFAULT_TIME_FORMAT;
  22. return $timeFormat;
  23. }
  24. public function getDateFormat() {
  25. $dateFormat = $this->wp->getOption('date_format');
  26. if (empty($dateFormat)) $dateFormat = self::DEFAULT_DATE_FORMAT;
  27. return $dateFormat;
  28. }
  29. public function getCurrentTime($format=false) {
  30. if (empty($format)) $format = $this->getTimeFormat();
  31. return $this->wp->currentTime($format);
  32. }
  33. public function getCurrentDate($format=false) {
  34. if (empty($format)) $format = $this->getDateFormat();
  35. return $this->getCurrentTime($format);
  36. }
  37. public function formatTime($timestamp, $format=false) {
  38. if (empty($format)) $format = $this->getTimeFormat();
  39. return date($format, $timestamp);
  40. }
  41. public function formatDate($timestamp, $format=false) {
  42. if (empty($format)) $format = $this->getDateFormat();
  43. return date($format, $timestamp);
  44. }
  45. /**
  46. * Generates a list of time strings within an interval,
  47. * formatted and mapped from DEFAULT_TIME_FORMAT to WordPress time strings.
  48. */
  49. public function getTimeInterval(
  50. $startTime='00:00:00',
  51. $timeStep='+1 hour',
  52. $totalSteps=24
  53. ) {
  54. $steps = [];
  55. $formattedTime = $startTime;
  56. $timestamp = strtotime($formattedTime);
  57. for ($step = 0; $step < $totalSteps; $step += 1) {
  58. $formattedTime = $this->formatTime($timestamp, self::DEFAULT_TIME_FORMAT);
  59. $labelTime = $this->formatTime($timestamp);
  60. $steps[$formattedTime] = $labelTime;
  61. $timestamp = strtotime($timeStep, $timestamp);
  62. }
  63. return $steps;
  64. }
  65. }