[wp-trac] [WordPress Trac] #53294: Adding filter hook to current_time() function
WordPress Trac
noreply at wordpress.org
Sat May 29 06:22:47 UTC 2021
#53294: Adding filter hook to current_time() function
---------------------------+-----------------------------
Reporter: wpvar | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Date/Time | Version: trunk
Severity: normal | Keywords: has-patch
Focuses: accessibility |
---------------------------+-----------------------------
current_time function uses default PHP class DateTime to generate dates or
stamp, For local developers there is no option to modify dates that
current_time returns, Therefore returned strings or stamps will always be
according to gregorian calendar.
{{{#!php
<?php
function current_time( $type, $gmt = 0 ) {
// Don't use non-GMT timestamp, unless you know the difference and
really need to.
if ( 'timestamp' === $type || 'U' === $type ) {
return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' )
* HOUR_IN_SECONDS );
}
if ( 'mysql' === $type ) {
$type = 'Y-m-d H:i:s';
}
$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
$datetime = new DateTime( 'now', $timezone );
return $datetime->format( $type );
}
}}}
By adding new filter hook, it will help local developers to convert
gregorian dates to other calendar types as well:
{{{#!php
<?php
function current_time( $type, $gmt = 0 ) {
// Don't use non-GMT timestamp, unless you know the difference and
really need to.
if ( 'timestamp' === $type || 'U' === $type ) {
return $gmt ? time() : time() + (int) ( get_option(
'gmt_offset' ) * HOUR_IN_SECONDS );
}
if ( 'mysql' === $type ) {
$type = 'Y-m-d H:i:s';
}
$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
$datetime = new DateTime( 'now', $timezone );
$datetime = $datetime->format( $type );
/**
* Filters current datetime based on the type.
*
* @since 5.8.0
*
* @param int|string $datetime Integer if $type is 'timestamp',
string otherwise.
* @param int $type Type of time to retrieve.
* @param int|bool $gmt Whether to use GMT timezone.
* @param DateTimeZone $timezone Timezone.
*/
$datetime = apply_filters( 'current_time', $datetime, $type, $gmt,
$timezone );
return $datetime;
}
}}}
Since many plugin and theme developers use current_time directly to
generate current dates, It will enhance alternative calendars usability in
WordPress for local and non-gregorian territories users. I think best
approach here could be for local developers to ignore mysql and timestamp
fields and only convert other formats based on different scenarios and
values that hook will return.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/53294>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list