AutocompletePostListLoader.php
2 months ago
DateTime.php
1 year ago
Emoji.php
2 months ago
Functions.php
1 week ago
Notice.php
3 years ago
Posts.php
3 years ago
Readme.php
3 years ago
index.php
3 years ago
DateTime.php
103 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\WP; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class DateTime { |
| 11 | |
| 12 | const DEFAULT_DATE_FORMAT = 'Y-m-d'; |
| 13 | const DEFAULT_TIME_FORMAT = 'H:i:s'; |
| 14 | const DEFAULT_DATE_TIME_FORMAT = 'Y-m-d H:i:s'; |
| 15 | |
| 16 | /** @var WPFunctions */ |
| 17 | private $wp; |
| 18 | |
| 19 | public function __construct( |
| 20 | ?WPFunctions $wp = null |
| 21 | ) { |
| 22 | if ($wp === null) { |
| 23 | $wp = new WPFunctions(); |
| 24 | } |
| 25 | $this->wp = $wp; |
| 26 | } |
| 27 | |
| 28 | public function getTimeFormat() { |
| 29 | $timeFormat = $this->wp->getOption('time_format'); |
| 30 | if (empty($timeFormat)) $timeFormat = self::DEFAULT_TIME_FORMAT; |
| 31 | return $timeFormat; |
| 32 | } |
| 33 | |
| 34 | public function getDateFormat() { |
| 35 | $dateFormat = $this->wp->getOption('date_format'); |
| 36 | if (empty($dateFormat)) $dateFormat = self::DEFAULT_DATE_FORMAT; |
| 37 | return $dateFormat; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string $format Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U', |
| 42 | * or PHP date format string (e.g. 'Y-m-d'). |
| 43 | * |
| 44 | * @return int|string Integer if `$format` is 'timestamp' or 'U' |
| 45 | */ |
| 46 | public function getCurrentTime(string $format = '') { |
| 47 | if (empty($format)) $format = $this->getTimeFormat(); |
| 48 | return $this->wp->currentTime($format); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param string $format Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U', |
| 53 | * or PHP date format string (e.g. 'Y-m-d'). |
| 54 | |
| 55 | * @return int|string Integer if `$format` is 'timestamp' or 'U' |
| 56 | */ |
| 57 | public function getCurrentDate(string $format = '') { |
| 58 | if (empty($format)) $format = $this->getDateFormat(); |
| 59 | return $this->getCurrentTime($format); |
| 60 | } |
| 61 | |
| 62 | public function formatTime($timestamp, $format = false) { |
| 63 | if (empty($format)) $format = $this->getTimeFormat(); |
| 64 | |
| 65 | return date($format, $timestamp); |
| 66 | } |
| 67 | |
| 68 | public function formatDate($timestamp, $format = false) { |
| 69 | if (empty($format)) $format = $this->getDateFormat(); |
| 70 | |
| 71 | return date($format, $timestamp); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Generates a list of time strings within an interval, |
| 76 | * formatted and mapped from DEFAULT_TIME_FORMAT to WordPress time strings. |
| 77 | */ |
| 78 | public function getTimeInterval( |
| 79 | $startTime = '00:00:00', |
| 80 | $timeStep = '+1 hour', |
| 81 | $totalSteps = 24 |
| 82 | ) { |
| 83 | $steps = []; |
| 84 | |
| 85 | $formattedTime = $startTime; |
| 86 | $timestamp = strtotime($formattedTime); |
| 87 | |
| 88 | for ($step = 0; $step < $totalSteps; $step += 1) { |
| 89 | $formattedTime = $this->formatTime($timestamp, self::DEFAULT_TIME_FORMAT); |
| 90 | $labelTime = $this->formatTime($timestamp); |
| 91 | $steps[$formattedTime] = $labelTime; |
| 92 | |
| 93 | $timestamp = strtotime($timeStep, $timestamp); |
| 94 | } |
| 95 | |
| 96 | return $steps; |
| 97 | } |
| 98 | |
| 99 | public function getCurrentDateTime(): \DateTime { |
| 100 | return new \DateTime("now", wp_timezone()); |
| 101 | } |
| 102 | } |
| 103 |