Working with date and time
-
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?
- The topic ‘Working with date and time’ is closed to new replies.