PHP date script returns warning message

(I want to apologize in advance if I am posting in the wrong area; I believe the issue may be server-related but I just don’t know. I will repost in the recommended area. Thanks for looking.)

I have jumped into the first chapter of “Build Your Own Databse Driven Website with PHP and MySQL” and have come across my first minor setback. I have installed the server, database and PHP support with no trouble. When I ran the first test script I received an additional “warning” message that was not mentioned in the book. (See attached file)

The code used was the simple

 echo  date('l, F dS Y.'); 

Everything else in the file was just the standard html page markup.

I did not encounter a time zone setting during the installation of my server software, but the message indicates the issue lies in that area. Clearly I’m very new to this, and any hints or nudges in the right direction would be greatly appreciated. Thank you so much!

Dear I have used the same function u have provided and its out come was this “Tuesday, December 07th 2010”! There is nothing wrong with this function!

See yourself here

I usually use this file for testing code: So no worries of other stuff

In your php.ini file looke for a line that looks something like:

date.timezone = UTC

Chances are that either you have the line commented out or you don’t have a value set.

Still, it does throw an errors if you have error reporting on, so there must be a reason. I’ve been seeing this too, so would like to know if there’s a better way to do dates these days.

Either you set it in your php.ini default time zone as suggested by SpacePhoenix or you can use the function date_default_timezone_set() as suggested in your error/warning message. See the example and set the timezone string to fit your locality. See the supported timezone strings here

Hmm, not used to playing with php.ini, but on taking a look I couldn’t see that line listed anywhere. (Even did a search of the page.)

What version of PHP you are in ralph? As far as I know, the line must exist but it could be without a value assigned. I have PHP 5.3.2 and I can find the following just below the list of extensions:

; Defines the default timezone used by the date functions
date.timezone =Asia/Katmandu

;date.default_latitude = 31.7667

;date.default_longitude = 35.2333

;date.sunrise_zenith = 90.583333

;date.sunset_zenith = 90.583333

The value ‘Asia/Katmandu’ was set by me after the installation. I suspect you searched the whole line ‘date.timezone = UTC’, try searching only ‘date.timezone’ instead then you can find I think.

5.2.8. I’m probably looking in the wrong place, because there’s no mention of date at all. A hundred other things, but no date.

Are you using some installer packages like WAMP/XAMPP or you have your own PHP?

Nah, I actually have a VPS, but I’m pretty clueless about managing it.

I checked php.ini on my local machine, comparing 5.2 and 5.3.

date.timezone =

appears in 5.3 but not in 5.2, so it must be new. I’ll have to check now, but it’s possible that the error message I’ve been getting (and which was reported by the OP) is only on hosts with php 5.3 installed. I’ll have to check that.

I’m on PHP 5.2.13 and my php.ini has the following, just below the list of available extensions

; Module Settings ;

; Defines the default timezone used by the date functions
date.timezone = Australia/Queensland


I know it’s ugly, but it works :wink:
Sets the default timezone to whatever the server has configured and will set it to UTC if there is no timezone configured on your server.

Hmm, finally got access to php.ini on my server and that code is not there (version 5.2.8) so it must be a newer feature.

I want to give a real quick Thank You! for all the fast and helpful responses. This has been a great first experience with this forum.

The issue did lie with the date.timezone line being commented out. (And I am running 5.3 – ralph I’m not sure if there is a way for me to check your page but I can try.) I just searched for date. and found it on the second one. I added America/New_York and all is well. One thing I will say, though, is that I had to restart my Apache service to see the change. Refreshing the page doesn’t do it – at least it didn’t for me. Hopefully this will help anyone else who has this problem.

SpacePhoenix thanks so much for the link to the timezones; that was a huge help! I downloaded the latest database (via PECL) but still need to look through them and figure out where they need to be installed. I’m assuming the PHP folder but not sure if it’s the root or a subfolder.

So I’m off to the next chapter! Thanks again to all of you! I’ll definitely be back again.

Immerse, I wanted to give this a shot but I am not sure where to put it. I tried it in the php file (in between the <?php… ?> tags) but that didn’t work. Should this go in the php.ini file or somewhere else?

Better to put this code on top of the script (or at least before you use any date time functions). Not in the php.ini.

This problem arose with I upgraded to PHP 5.3 and using function_date();

PHP Changelog
Version Description
5.0.0 E_STRICT introduced (not part of E_ALL).
5.2.0 E_RECOVERABLE_ERROR introduced.
5.3.0 E_DEPRECATED and E_USER_DEPRECATED introduced.

From memory a KLUDGE to hide this warning:

error_level(~ E_DEPRECATED);

Instead of using the KLUDGE it is better to set date_default_timezone_set(date_default_timezone_get());

Hm well I put it above the echo date line but the message only changed to include the new function as part of the error (after “Warning:”). I also went back and commented the date line in the .ini file to make sure the refreshed page was just relying on the new code. If it helps I’ve attached a screen shot of the code. Thanks for your patience with me.

Can you try this script and supply the results?

    // try with and without this next line remarked
    // error_reporting(~ E_DEPRECATED);

    echo '&lt;br /&gt;',
    $result = date_default_timezone_set(date_default_timezone_get()); 
    echo '&lt;br /&gt;Result: ',
    echo  '&lt;br /&gt;', date('l, F dS Y.');