Friday, November 19, 2021

[SOLVED] PHP date function is not respecting date_default_timezone_set

Issue

I'm trying to figure out why the date function is not respecting the default php timezone. This is my test script:

<?php

echo( "date_default_timezone_get: " . date_default_timezone_get(). "\n" );
echo( "date: " . date(DATE_RFC2822) . "\n" . "gmdate: " . gmdate(DATE_RFC2822) . "\n" );
echo( "date_default_timezone_set( 'UTC' ): " . date_default_timezone_set( 'UTC' ) . "\n" );
echo( "date: " . date(DATE_RFC2822) . "\n" . "gmdate: " . gmdate(DATE_RFC2822) . "\n" );
echo( "date_default_timezone_get: " . date_default_timezone_get() );
exit;

it's output is:

date_default_timezone_get: Europe/Rome
date: Thu, 08 Jul 2021 14:30:40 +0200
gmdate: Thu, 08 Jul 2021 12:30:40 +0000
date_default_timezone_set( 'UTC' ): 1
date: Thu, 08 Jul 2021 14:30:40 +0200
gmdate: Thu, 08 Jul 2021 12:30:40 +0000
date_default_timezone_get: UTC

As you can see, when I set the default timezone to UTC the date function won't change it's output

I'm using php fpm 7.4 on a debian 10 machine

> php -v
PHP 7.4.20 (cli) (built: Jun  4 2021 23:17:27) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.20, Copyright (c), by Zend Technologies

Solution

Solved by updating to the minor version of PHP



Answered By - Emax