Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#48976 closed defect (bug) (invalid)

Current_time

Reported by: autotutorial's profile autotutorial Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.3
Component: Date/Time Keywords:
Focuses: Cc:

Description

<?php
function current_time( $type, $gmt = 0 ) {
    // Don't use non-GMT timestamp, unless you know the difference and really need to.
 
    if ( 'mysql' === $type ) {
        $type = 'Y-m-d H:i:s';
    }
 
    $timezone = $gmt ? new DateTimeZone( 'UTC' ) : new DateTimeZone(wp_timezone());
    $datetime = new DateTime( 'now', $timezone );
 
    if ( 'timestamp' === $type || 'U' === $type ) {
        //for UTC offset is 0
        return time()+$datetime->getOffset();
    }
    return $datetime->format( $type );
}

Add correct DateTimeZone for $gmt 0 and $timestamp or U now have your appropiate offset.

Change History (10)

#1 @SergeyBiryukov
5 years ago

  • Component changed from Revisions to Date/Time
  • Keywords needs-codex removed

#2 @Rarst
5 years ago

  • Keywords reporter-feedback added

Sorry, I don't quite follow your report.

Could you please elaborate what do you think the bug or issue otherwise is? Do you suggest to rewrite the function? For what reason?

#3 @autotutorial
5 years ago

I certainly elaborate my report, I admire WordPress and often I copy the code for my personal projects, I observed the wp_timezone function returns a text string or a string hh: mm but keep in mind that the hour is a float and not int as it does current_time but especially for the timestamp does not take into account the timezone string, if the timezone string is present the almost numerical offset has a value of 0.
With php 7.3 I get an error if the DateTimeZone is not an object but a string.

Time never stands still, in reality if you work with time zones that are not directly created by php, it is useful to check the DateTimeZone string with the WordPress string, differently when working dynamically with the php database timezone table ( there should not be a timezone conversion) also previous versions use daylight saving time incorrectly, it would be nice to add this also in WordPress.
Another aspect is if I use php with local settings or the variable tz who knows how DateTime works? :D

Last edited 5 years ago by autotutorial (previous) (diff)

#4 @Rarst
5 years ago

but keep in mind that the hour is a float and not int as it does current_time but especially for the timestamp does not take into account the timezone string, if the timezone string is present the quasi numerical offset has a value of 0

Sorry, I don't follow this.

Could you maybe provide a code example of what you are trying to do and what happens instead of what you expect?

With php 7.3 I get an error if the DateTimeZone is not an object but a string

Could you please provide example that causes the error and what your time zone settings are?

it is useful to check the DateTimeZone string with the WordPress string, differently when working dynamically with the php database timezone table ( there should not be a timezone conversion) also previous versions use daylight saving time incorrectly

I don't follow this part. Yes, historically there are a lot of DST bug caused by use of WP timestamps (sum of a Unix timestamp with time zone offset). Historically WP calculated these by adding offset at current time, which might be wrong, but doing this is altogether wrong.

This behavior is preserved for backwards compatibility reasons and optimal action is to use real Unix timestamps and proper DateTimeImmutable/DateTimeZone objects.

Another aspect is if I use php with local settings or the variable tz who knows how DateTime works?

The point of new API is that DateTimeZone works regardless of underlying settings.

Version 0, edited 5 years ago by Rarst (next)

#5 @autotutorial
5 years ago

https://developer.wordpress.org/reference/functions/current_time/

<?php
// Bad
return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );

I haven't checked in WordPress but in my code.

<?php
$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();

This for $gmt = 0 create string, not object.

Thanks for your answers, bad conversion DateTimeZone https://www.php.net/manual/en/class.datetimezone.php#122558

Last edited 5 years ago by autotutorial (previous) (diff)

#6 @Rarst
5 years ago

Why do you think that line is "Bad"?

This for $gmt = 0 create string, not object.

I don't know how would that produce a string. What exactly does does var_dump( wp_timezone() ); gives you? What are your settings?

#7 @autotutorial
5 years ago

var_dump(new DateTime( 'now', 'Europe/Berlin'), new DateTime( 'now', new DateTimeZone('Europe/Berlin')));
<br />
<b>Fatal error</b>: Uncaught TypeError: DateTime::construct() expects parameter 2 to be DateTimeZone, string given in /home/user/prova.php:234
Stack trace:
#0 /home/user/prova.php(234): DateTime-&gt;
construct('now', 'Europe/Berlin')
#1 {main}
thrown in <b>/home/user/prova.php</b> on line <b>234</b><br />

#8 @Rarst
5 years ago

new DateTime( 'now', 'Europe/Berlin') is wrong because DateTime constructor does not accept time zone as string.

I don't see how this relates to core code?

#9 @autotutorial
5 years ago

In the core I don't see adding an object but simply a string. wp_timezone alias of wp_timezone_string the value is string, in the current_time code is added to the DateTime class as a string.

Now think of get_option ('gmt_offset');
i have string timezone Europe/Berlin get_option('timezone_string') mind offset get_option('gmt_offset') is equal to 0 or I have 1.30 the conversion into integer loses all after the decimal point.
I apologize, I checked and everything works correctly, my error was to expect a string from wp_timezone instead it is converted into a DateTimeZone object, while even if I don't have gmt_offset in the WordPress database it converts it to the correct value.
Thank you for your time.

Last edited 5 years ago by autotutorial (previous) (diff)

#10 @Rarst
5 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.