Avatar of Mark Brady
Mark Brady
Flag for United States of America asked on

php error_log date and time format settings

I know I should know this but it's driving me nuts. How do you set the date and time format in php error_log(). I have set the default_time_zone setting and made sure the system date and time zones are correct. Here is what the error log puts out

[30-Jun-2012 01:17:09 UTC] test
[30-Jun-2012 01:17:10 UTC] test
[30-Jun-2012 01:32:27 UTC] test
[30-Jun-2012 01:32:32 UTC] test..... etc

I want it to put out

[2012-06-30 22:17:09] test
[2012-06-30 22:17:09] test
[2012-06-30 22:17:09] test
[2012-06-30 22:17:09] test.... etc

I would prefer it doesn't use UTC as I have set my timezone to 'America/New_York'

We are in Florida so it should be reporting as EST but the most important part is to have the error_log file record the system time and in the correct format ie: 'Y-m-d H:i:s'

Thanks for the help. I should know this stuff but for years I have been using an error log at work that records the date properly. I'm just not at work now :)
Scripting LanguagesPHP

Avatar of undefined
Last Comment
Mark Brady

8/22/2022 - Mon

You can either set default time zone in ini
date.timezone = 'America/New_York'
or you can add a line in the script where you are using date

and finally put the date format explicitly in the error_log
error_log('['.date('YYYY-MM-dd HH:mm:ss').']'.$msg.........)

sorry that would be
date('Y-m-d H:i:s')
Mark Brady

Nope that's no good. Like I said, on my work system I don't need to format the date to use error log.

err_log('test'); will print [2012-06-30 22:17:09] test

Why should I have to add the date format at home?

Besides, your way would produce this result

[30-Jun-2012 01:17:09 UTC][2012-06-30 01:17:09] test

Yuk but thanks anyway

There is a way to explicitly set the format that is used by error_log. It may be a computer system setting for all I know but I use Ubuntu and I can't see how to get the date formatted like that anywhere.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mark Brady

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Mark Brady

I did not get a satisfatory result from this question. I wrote my own error log handler and have it customized to suit my needs. I did spend a lot of time looking through Google to find a solution but it seems php don't even control the error log apart from the filename and sending entries into it.

It would be a far wiser solution for PHP to get rid of the external companies software and write their own error handler to deal with error_log() entries. That way we could format the date at least to suit our particular situations.