• bitcomplex

    (@bitcomplex)


    I work in an environment where we try to code everything as de-coupled from WordPress as possible even though everything we do resides in a huge company specific WordPress plugin.

    I have a hard time understanding the docs and other forum threads regarding date and time functions and what the actual correct way to use them is. My goal is to be able to use DateTime/DateTimeImmutable/date/strtotime etc as close to pure php as possible.

    So my thought so far goes along the way of this:

    <?php
    namespace CompanyNS;
    
    use DateTimeZone;
    
    class DateTime extends \DateTime
    {
    	public function __construct($datetime = 'now', DateTimeZone $timezone = null)
    	{
    		if (is_null($timezone)) {
    			if ($datetime === "now") {
    				$datetime = "@" . current_time("timestamp");
    			} else {
    				$datetime = current_time($datetime);
    			}
    		}
    		parent::__construct($datetime, $timezone);
    	}
    }

    The same goes for DateTimeImmutable. Actually never used the $timezone argument so don’t know if it is correct to skip current_time if $timezone is specified.

    Further we already have a way of overriding global scoped functions and variables through a proxy-class (which we use for certain WordPress functions). Thinking of putting date and strtotime in the proxy-class (calling the proxy will run the method if it exists and otherwise run the global function – so it is easy to refactor to use the native function by just deleting the implementation from the proxy)

    
    ...
    	public function strtotime(string $dateFormatString): int
    	{
    		return current_time($dateFormatString);
    	}
    
    	public function date(string $dateFormatString): string
    	{
    		return date($dateFormatString, current_time("timestamp"));
    	}
    ...
    

    Is current_time in these settings analogous to strtotime? Am I missing something?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Joy

    (@joyously)

    To answer this, you would need different things for different WP versions.
    The date time changes went into WP 5.3.

    Date/Time component improvements in WordPress 5.3


    You can see the details at https://github.com/Rarst/wordpress-develop/issues?q=is%3Aissue+is%3Aclosed as well as Trac tickets about the date.

    WP has an option for the admin to choose the time zone that the site should reflect. But not all date or time manipulations are for the date to show on the page. Some can be for computing date differences or time offset from the visitor or date that is stored in the database. The context matters because the time zone of the server can be different from the time zone that should be shown on the page or the time zone of the visitor.

    Thread Starter bitcomplex

    (@bitcomplex)

    I have a hard time understanding the docs. It says that it is recommended to use DateTimeImmutable, but using it gives me the wrong time (I want the server time always, never what the wp admin has set) So my context is that I do not want to be left at the mercy of any wp admin. And I preferably do not want to use any wordpress specific functions.

    Joy

    (@joyously)

    See this function?
    https://www.php.net/manual/en/function.date-default-timezone-set.php

    It is called in wp-settings.php.
    date_default_timezone_set( 'UTC' );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Working with date and time’ is closed to new replies.