PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.6
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / helpers / date-helper.php

date-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.6, at src/helpers/date-helper.php

121 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Helpers;
4
5 use DateTime;
6 use DateTimeZone;
7 use Exception;
8
9 /**
10 * A helper object for dates.
11 */
12 class Date_Helper {
13
14 /**
15 * Convert given date string to the W3C format.
16 *
17 * If $translate is true then the given date and format string will
18 * be passed to date_i18n() for translation.
19 *
20 * @param string $date Date string to convert.
21 * @param bool $translate Whether the return date should be translated. Default false.
22 *
23 * @return string Formatted date string.
24 */
25 public function mysql_date_to_w3c_format( $date, $translate = false ) {
26 return \mysql2date( \DATE_W3C, $date, $translate );
27 }
28
29 /**
30 * Formats a given date in UTC TimeZone format.
31 *
32 * @param string $date String representing the date / time.
33 * @param string $format The format that the passed date should be in.
34 *
35 * @return string The formatted date.
36 */
37 public function format( $date, $format = \DATE_W3C ) {
38 if ( ! \is_string( $date ) ) {
39 return $date;
40 }
41
42 $immutable_date = \date_create_immutable_from_format( 'Y-m-d H:i:s', $date, new DateTimeZone( 'UTC' ) );
43
44 if ( ! $immutable_date ) {
45 return $date;
46 }
47
48 return $immutable_date->format( $format );
49 }
50
51 /**
52 * Formats the given timestamp to the needed format.
53 *
54 * @param int $timestamp The timestamp to use for the formatting.
55 * @param string $format The format that the passed date should be in.
56 *
57 * @return string The formatted date.
58 */
59 public function format_timestamp( $timestamp, $format = \DATE_W3C ) {
60 if ( ! \is_string( $timestamp ) && ! \is_int( $timestamp ) ) {
61 return $timestamp;
62 }
63
64 $immutable_date = \date_create_immutable_from_format( 'U', $timestamp, new DateTimeZone( 'UTC' ) );
65
66 if ( ! $immutable_date ) {
67 return $timestamp;
68 }
69
70 return $immutable_date->format( $format );
71 }
72
73 /**
74 * Formats a given date in UTC TimeZone format and translate it to the set language.
75 *
76 * @param string $date String representing the date / time.
77 * @param string $format The format that the passed date should be in.
78 *
79 * @return string The formatted and translated date.
80 */
81 public function format_translated( $date, $format = \DATE_W3C ) {
82 return \date_i18n( $format, $this->format( $date, 'U' ) );
83 }
84
85 /**
86 * Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
87 *
88 * @return int The current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
89 */
90 public function current_time() {
91 return \time();
92 }
93
94 /**
95 * Check if a string is a valid datetime.
96 *
97 * @param string $datetime String input to check as valid input for DateTime class.
98 *
99 * @return bool True when datetime is valid.
100 */
101 public function is_valid_datetime( $datetime ) {
102 if ( $datetime === null ) {
103 /*
104 * While not "officially" supported, `null` will be handled as `"now"` until PHP 9.0.
105 * @link https://3v4l.org/tYp2k
106 */
107 return true;
108 }
109
110 if ( \is_string( $datetime ) && \substr( $datetime, 0, 1 ) === '-' ) {
111 return false;
112 }
113
114 try {
115 return new DateTime( $datetime ) !== false;
116 } catch ( Exception $exception ) {
117 return false;
118 }
119 }
120 }
121