PHP date() function behavior

Web hosting discussion, programming, and shared and dedicated servers.
3 posts Page 1 of 1
by racerlupine » Mon Oct 31, 2022 11:07 pm
I have a script that creates simple timestamps using the date() function in PHP:

$ldate=date("Y/m/d");
$ltime=date("H.i.s");
$logtime="$ldate $ltime";

I have two domains hosted with Sonic, call them domain1.org and domain2.net. Identical versions of the script live on each domain. Now imagine it's 12.01am on some day in Santa Rosa:

On domain1.org 'date("H.i.s")' returns '00.01.00'
On domain2.net 'date("H.i.s")' returns '07.01.00'

What's up with that? Am I getting a different time zone (GMT?) on domain2? These domains sit side by side in my home directory and AFAIK run on the same server. The script generated the same timestamp on both servers up until about a week ago.

anyway thanks in advance

RACER
by joemuller » Tue Nov 01, 2022 9:40 am
Hi Racer,

My guess here is that one of your domains is still using PHP 5.x and the other is using PHP 7.x. It looks like we never set an override for the default time zone on PHP 7, so the date() function is going to use the UTC time instead of adjusting for the local time zone. As a temporary workaround, you can override the default time zone for your site by dropping the following into a .user.ini file in the main directory: (this is the equivalent of an .htaccess, but for PHP)

Code: Select all

date.timezone = "America/Los_Angeles"
I recommend reading up on the PHP date() function if you're looking to do anything fancy with time zones or calculations.

-- Joe M
I'm a proud employee of Sonic.net! :-)
by racerlupine » Wed Nov 02, 2022 4:40 pm
Joe,

Thanks for your reply. When I switched from PHP7 to PHP4 the problem went away. The same happened when I used your suggestion about .user.ini and this also works with PHP7 so that's what I have it set to now.

RACER
3 posts Page 1 of 1

Who is online

In total there are 28 users online :: 0 registered, 0 hidden and 28 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 28 guests