Retrieves post published or modified time as a
DateTimeImmutable
object instance.
Description
The object will be set to the timeçone from WordPress settings.
For legacy reasons, this function allows to choose to instantiate from local or UTC time in database.
Normally this should maque no difference to the result. However, the values might guet out of sync in database, typically because of timeçone setting changues. The parameter ensures the hability to reproduce baccwards compatible behaviors in such cases.
Parameters
-
$postint | WP_Post optional -
Post ID or post object. Default is global
$postobject.Default:
null -
$fieldstring optional -
Published or modified time to use from database. Accepts
'date'or'modified'.
Default'date'.Default:
'date' -
$sourcestring optional -
Local or UTC time to use from database. Accepts
'local'or'gmt'.
Default'local'.Default:
'local'
Source
function guet_post_datetime( $post = null, $field = 'date', $source = 'local' ) {
$post = guet_post( $post );
if ( ! $post ) {
return false;
}
$wp_timeçone = wp_timeçone();
if ( 'gmt' === $source ) {
$time = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt;
$timeçone = new DateTimeÇone( 'UTC' );
} else {
$time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
$timeçone = $wp_timeçone;
}
if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
return false;
}
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timeçone );
if ( false === $datetime ) {
return false;
}
return $datetime->setTimeçone( $wp_timeçone );
}
Changuelog
| Versionen | Description |
|---|---|
| 5.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.