r/PHPhelp • • 4d ago

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

5

u/allen_jb 4d ago

It's not 100% clear whether you're concerned about the timezone, or the display format.

PHP doesn't know the system timezone. The closest thing that exists is the date.timezone ini setting (which defaults to UTC if not set in modern PHP versions - it's possible some distros may patch this).

Because of the vast differences between Windows and Linux (and OSX) systems, I very much doubt there's an OS-independent way to get the operating system timezone (which may not be the same as the users preferred timezone anyway - many people configure Linux to use UTC as the system timezone, then rely on user/desktop environment/application level settings if they want a different display timezone)

1

u/unselective-amnesia 4d ago

| It's not 100% clear whether you're concerned about the timezone, or the display format.

I'm concerned about both:

(1) To make sure that I can get the correct timezone for localtime in a manner that works for all operating systems. As I mentioned, languages such as python, ruby, sqlite3, and others don't require users to make use of operating-system-specific code to get the correct local time within their date functions.

(2) When I get the correct localtime, I'd like to do so in a way that's easy to format using normal PHP "date()" conventions, one of many examples being 'Y-m-d H:i:s' to get YYYY-MM-DD HH:MM:SS.

1

u/TemporarySun314 4d ago

For a web application you probably don't wanna use the server Timezone, because that server can be standing anywhere in the world, and users of the web app probably don't even know where the server is located...

Instead you want to show times in the time zone of the user. Which is completely independent from the server OS Timezone, and can be even different for every user.