• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/helpers/date-helper.php
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 ) {
18✔
38
                if ( ! \is_string( $date ) ) {
18✔
39
                        return $date;
8✔
40
                }
41

42
                $immutable_date = \date_create_immutable_from_format( 'Y-m-d H:i:s', $date, new DateTimeZone( 'UTC' ) );
10✔
43

44
                if ( ! $immutable_date ) {
10✔
45
                        return $date;
6✔
46
                }
47

48
                return $immutable_date->format( $format );
4✔
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 ) {
8✔
60
                if ( ! \is_string( $timestamp ) && ! \is_int( $timestamp ) ) {
8✔
61
                        return $timestamp;
2✔
62
                }
63

64
                $immutable_date = \date_create_immutable_from_format( 'U', $timestamp, new DateTimeZone( 'UTC' ) );
6✔
65

66
                if ( ! $immutable_date ) {
6✔
67
                        return $timestamp;
2✔
68
                }
69

70
                return $immutable_date->format( $format );
4✔
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 ) {
2✔
82
                return \date_i18n( $format, $this->format( $date, 'U' ) );
2✔
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 ) {
10✔
102
                if ( $datetime === null ) {
10✔
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;
2✔
108
                }
109

110
                if ( \is_string( $datetime ) && \substr( $datetime, 0, 1 ) === '-' ) {
8✔
111
                        return false;
2✔
112
                }
113

114
                try {
115
                        return new DateTime( $datetime ) !== false;
6✔
116
                } catch ( Exception $exception ) {
2✔
117
                        return false;
2✔
118
                }
119
        }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc