r/PHPhelp • • Oct 08 '26

Generic formatted PHP localtime function that works without modification under all operating systems?

I'm trying to write a PHP function that will return formatted local time on all operating systems (Windows, MacOS, all flavors of Linux, etc.), and which will correctly run without any operating-system-dependent changes to the code. And I want it to automatically figure out the local time zone without making use of any operating-system-specific code.

By "formatted", I mean that it will honor all PHP date/time formatting directives, such as 'Y-m-d H:i:s' (to name but one of all the possibilities).

I can't use IntlDateFormater, because it requires the local time zone identifier to be passed within the constructor, as in the following example:

$fmt = new IntlDateFormatter(
    'en_US',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN
);

This requires "America/Los_Angeles" to be passed to the formatter, but I want this timezone identifier to be determined in PHP automatically at run time in a way that is totally independent of the operating system, so that my PHP localtime formatting function will run on all operating systems without any code changes.

And I don't want this PHP function to have to shell out to run the "date" command of the operating system. This is because PATH must be made available to PHP in order for "date" to be found, and the contents of this PATH could differ between different OS configurations ... and some people might even have put a non-standard "date" executable in their PATH. I don't want the users of this PHP script to have to fiddle with PATH before this function will work.

This kind of system-independent localtime formatting is trivially easy in languages such as python, ruby, and others, as well as within sqlite3 and I think other SQL interpreters, without PATH being known nor even being accessible by the calling program. I'm wishing and hoping that this localtime formatting also can be done within PHP without a bunch of complicated, convoluted, error-prone logic ... but I'm not optimistic.

Any ideas?

2 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/unselective-amnesia Oct 08 '26

Ah ... good idea! Thank you. I'll check out that same information, and I hope that it indeed provides the answer that I'm looking for.

Just keep in mind that I wrote the following in my initial message:

"I'm wishing and hoping that this localtime formatting also can be done within PHP without a bunch of complicated, convoluted, error-prone logic ..."

I'll see how complex and convoluted the translation of this Python methodology to PHP will turn out to be.

1

u/MrJingleJangle Oct 08 '26

Two other suggestions, but in a longer timeframe, you could broach the idea with the PHP developers, or just go for it and submit code.

1

u/unselective-amnesia Oct 08 '26

Two other suggestions, but in a longer timeframe, you could broach the idea with the PHP developers, or just go for it and submit code.

Yes, if I ever find a way to accomplish this via PHP code, I indeed intend to submit it to the PHP developers as an enhancement request.

0

u/unselective-amnesia Oct 08 '26

I found the source code for Python's "localtime" function. Within this source code is the following comment:

The TZ environment variable should be specified in\n\

standard Unix timezone format as documented in the tzset man page\n\

(eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently\n\

fall back to UTC. If the TZ environment variable is not set, the local\n\

timezone is set to the systems best guess of wallclock time.

I don't run with the "TZ" environment variable set to anything, and Python still figures out the correct time zone to use for localtime determination ... and according to the quoted comment, this means that localtime determination is based upon "the systems best guess of wallclock time".

But I haven't found anything so far within the Python code base which explains how "the systems best guess of wallclock time" is determined when the "TZ" environment variable is not set.

I'll keep searching, but even if I eventually find this information, I'm not optimistic that I'll find anything that can be converted in a straightforward manner to PHP code.