• Resolved Guido

    (@guido07111975)


    Hi,

    While reading the thread about date/time improvements I notice it’s discouraged to use the current_time( 'timestamp' ).

    This function returns the local timestamp. I need that. But I cannot find a replacement for this, that returns a timestamp with local timezone included.

    Have tried the new current_datetime() function but that one does not return a timestamp. So by using strtotime() I can generate a timestamp:

    
    $obj = current_datetime();
    $array = get_object_vars($obj);
    $my_current_time = strtotime($array['date']);
    

    But timezone can be different when some plugin/theme sets another one by using function date_default_timezone_set(). So this I cannot fully trust.

    Who can help me with this?

    Guido

Viewing 13 replies - 16 through 28 (of 28 total)
  • Thread Starter Guido

    (@guido07111975)

    Hi again @autotutorial ,

    I’m trying to set the time at midnight, by using your code, but I’m not succeeding:

    
    $current_date_time = current_datetime();
    $current_date_time->setTime(0, 0);
    $local_time_midnight = $current_date_time->getTimestamp()+$current_date_time->getOffset();
    

    But still local timestamp is being returned.

    As far as I know the setTime is supported?

    Guido

    Thread Starter Guido

    (@guido07111975)

    This also returns a midnight timestamp depending on timezone:

    
    $current = current_time('Y-m-d');
    echo strtotime($current);
    

    Returns: 1577145600

    First thought whole current_time() function becomes deprecated in the future, but only the timestamp part is.

    Guido

    // DateTimeImmutable does not change use yours functions,
     this is the correct use see https://www.php.net/manual/en/class.datetimeimmutable.php for all list functions DateImmutable
    // I'm DateTimeImmutable and Retrive current date object
    // options 1 from PHP 5.5.0
    $array = current_datetime();
    // $array have Timezone Dashboard settings
    $output_date_string = $array->format( 'Y-m-d H:i:s' ); // here YYYY-MM-DD HH:MM:SS
    
    $timezone = wp_timezone(); // Retrive Dashboard timezone from WordPress 5.3
    // i'm DateTime
    $my_date = new DateTime( $output_date_string, $timezone );
    $my_date->modify( 'today midnight' );
    echo $my_date->format( 'Y-m-d H:i:s' );
    
    /*
    // options 2
    $timezone = wp_timezone();
    $my_date = new DateTime( 'today midnight', $timezone );
    echo $my_date->format( 'Y-m-d H:i:s' );
    
    // options 3
    $timezone = wp_timezone();
    $my_date = new DateTime( 'now', $timezone ); //php 7.1 microseconds parameter added
    $my_date->setTime( 0, 0, 0, 0); // php 7.1 four microseconds parameter added
    echo $my-date->format('Y-m-d H:i:s'); // for setTime
    
    //$var->setTimezone( $timezone ); from PHP 5.2.1
    */

    Never old PHP functions, strtotime or mktime etc.

    Thread Starter Guido

    (@guido07111975)

    Great! So I’m using an old function, which is replaced by better ones.

    I’m gonna choose option 2.. by the way today and midnight return the same time (00:00:00), according the PHP manual.

    Thanks!

    Guido

    yes you are in the right direction, the relative format is the one applied to strtotime or to the class DateTime DateTimeImmutable, remember not to insert a timezone when you have an offset from php 8.0 generates a warning.

    Note:

    The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

    Correct Use for always.

    $timezone = wp_timezone();
    $my_date = new DateTime( '@' . 1500000000);
    $my_date->setTimezone( $timezone ); // force use a un timezone or offset +01:00 , this offset php 5.5.0
    Thread Starter Guido

    (@guido07111975)

    Oh my..

    About your note:
    'today' becomes an UNIX timestamp, am I right?

    Why should I not use an old function such as strtotime? It works perfecly. And the current_time() function is not deprecated.

    
    $current = current_time('Y-m-d');
    echo strtotime($current);
    

    FYI: I only need the local today with time midnight (00:00:00) for my script.

    Guido

    https://www.php.net/manual/en/function.date-default-timezone-set.php
    date-default-timezone-set and https://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone date.timezone
    I can set a time zone, WordPress for over 10 years has been using default UTC, you can later select your time zone (timezone string or offset number) in the dashboard, the old php functions if they have a timezone other than UTC they produce a timestamp for that timezone while $array->getTimestamp() is the from UTC.
    Since you use an explicit timezone in DateTime your local time is always the correct one, it should also be remembered that Europe/Rome 2020-03-29 02:00:00 with the time zone Europe/Rome shows 2020-03-29 03:00: 00 Daylight saving time is in use (the timestamp is always UTC, the output for Daylight saving time changes).
    since current_time before WordPress 5.3 calculated a WP offset (he took UTC for granted and did the sum with the offset in the dashboard).
    As of WordPress 5.3 it is safe (now WordPress use DateTime class).
    Since in the past it has been used incorrectly it is right to remove current_time and use current_datetime.

    Thread Starter Guido

    (@guido07111975)

    Sorry, I’m not smart enough to fully understand this.

    I do want to use the new date/time functions of WP. A simple few lines of code that will be supported the next couple of years.

    I’m trying to get midnight timestamp (00:00:00) from the current_datetime() function but I’m not succeeding. I thought I could use setTime (because it’s accepted by the DateTimeImmutable class) but this is not working:

    
    $array = current_datetime();
    $localtime = $array->getTimestamp()+$array->getOffset();
    $localtime->setTime( 0, 0, 0, 0);
    echo $localtime;
    

    This results into an error.

    Guido

    function current_datetime() {
        return new DateTimeImmutable( 'now', wp_timezone() );
    }

    You can’t use DateTime functions for DateTimeImmutable, look at the first option of my code ??
    DateTimeImmutable https://www.php.net/manual/en/datetimeimmutable.settime.php do not modify DateTimeImmutable, convert it to DateTime.
    getTimestamp() should be used after setting midnight
    $object->getTimestamp();
    DateTime::getTimestamp
    DateTimeImmutable::getTimestamp
    DateTimeInterface::getTimestamp
    date_timestamp_get

    Learn to use DateTime, at the moment you cannot manage DateTimeImmutable, this code is only here for completeness.

    $array = current_datetime();
    $var = $array->setTime( 0, 0, 0, 0); // Work for version PHP 7.1 or above
    $localtime = $var->getTimestamp()+$var->getOffset();
    echo $localtime;

    Sorry
    replace $localtime = $var->getTimestamp()+$var->getOffset(); with $localtime = $var->getTimestamp() 23:00 UTC it’s correct, use timezone for consversion Europe/Rome 00:00 ?? (convert local to UTC with this timezone $object->setTimezone( new DateTimeZone( 'UTC' ) ); ).

    $array = current_datetime();
    $var = $array->setTime( 0, 0, 0, 0); // Work for version PHP 7.1 or above
    
    $timestamp = $var->getTimestamp();
    $my_date = new DateTime( '@' . $timestamp );
    $my_date->setTimezone( new DateTimeZone( 'UTC' ) ); //23:00 UTC correct
    
    //local time
    $timezone = $var->getTimezone(); //Europe/Rome offset +01:00
    $my_date2 = new DateTime( '@' . $timestamp );
    $my_date2->setTimezone( $timezone ); //00:00 Europe/Rome correct
    
    //Local time show UTC, never use it
    $new_timestamp = $timestamp+$var->getOffset();
    $my_date3 = new DateTime( '@' . $new_timestamp )
    $my_date3->setTimezone( new DateTimeZone( 'UTC' ) ); //00:00 correct conversion from local to UTC
    Thread Starter Guido

    (@guido07111975)

    Hi,

    So, this one seems to work fine to get the local midnight time of today:

    
    $array = current_datetime();
    $var = $array->setTime( 0, 0, 0, 0); // Work for version PHP 7.1 or above
    $localtime = $var->getTimestamp()+$var->getOffset();
    echo $localtime;
    

    I now don’t want to use DateTime anymore, because of this comment from you, regarding timezone.

    Guido

    I’ll explain, you can’t use a time zone on the DateTime object example

    // Bad for DateTime
    $object = new DateTime( '@' . 10000000, new DateTimeZone( 'Europe/Rome' ) );

    if the date has a timestamp (UTC) or a time zone expressed on the date.
    You can always use $object->setTimezone(new DateTimeZone('Europe/Rome')); regardless if you have an input with timestamp or time zone .. setTimezone is used exactly to set the timezone and cannot produce an error.

    Thread Starter Guido

    (@guido07111975)

    I’m a slow learner, but I now understand everyhting I need to know to complete/update my script. Many thanks and have a nice christmas!

    Guido

Viewing 13 replies - 16 through 28 (of 28 total)
  • The topic ‘Replacement for current_time( ‘timestamp’ )’ is closed to new replies.