Make WordPress Core


Ignore:
Timestamp:
08/09/2021 07:08:09 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Replace strftime() and gmstrftime() usage in unit tests.

Since PHP 8.1, the strftime() and gmstrftime() functions are deprecated:

The strftime() and gmstrftime() functions exhibit similar issues as strptime(), in that the formats they support, as well as their behavior, is platform-dependent. Unlike strptime(), these functions are available on Windows, though with a different feature set than on Linux. Musl-based distributions like Alpine do not support timezone-related format specifiers correctly. These functions are also locale-based, and as such may exhibit thread-safety issues.

date() or DateTime::format() provide portable alternatives, and IntlDateFormatter::format() provides a more sophisticated, localization-aware alternative.

Reference: PHP RFC: Deprecations for PHP 8.1: strftime() and gmstrftime()

The strftime() and gmstrftime() functions have been deprecated in favor of
date()/DateTime::format() (for locale-independent formatting) or
IntlDateFormatter::format() (for locale-dependent formatting).

Reference: PHP 8.1 Upgrade Notes.

Aside from one instance in SimplePie, the strftime() and gmstrftime() functions are only used within the test suite of WordPress to create formatted timestamps.

As the function is used in test code, this leads to test warnings like this on PHP 8.1:

Deprecated: Function strftime() is deprecated in path/to/tests/phpunit/tests/canonical/postStatus.php on line 37

These calls can all be safely converted to use a pattern along the lines of:

<?php
date_format( date_create( 'time phrase or timestamp' ), $format )

Other references:

Props jrf, SergeyBiryukov.
Fixes #53897.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php

    r50130 r51587  
    1919            $actual_status = $post_status;
    2020            if ( 'future' === $post_status ) {
    21                 $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
     21                $date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
    2222            } elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
    2323                $actual_status = 'publish';
     
    5252        $date = '';
    5353        if ( 'future' === $post_status ) {
    54             $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
     54            $date = date_format( date_create( '+1 year' ), 'Y-m-d H:i:s' );
    5555        }
    5656
Note: See TracChangeset for help on using the changeset viewer.