How to make PHP use my system time


Is it possible to to make PHP use server date and time instead of the timezone mentioned in php.ini file, i.e. if I change my server's date and time it should automatically use the same date and time.

Thanks in advance.
Vipin KumarSr. Network EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You can do this with a DateTimeZone object that establishes the preferred timezone.  You use the object as an injected dependency when you create an instance of the DateTime object.
$zoneobj = new DateTimeZone('America/New_York');
$dateobj = new DateTime('NOW', $zoneobj);

Open in new window

You can also do this by calling the date_default_timezone_set() function in procedural code.

I am not sure how to ask the server to tell you the timezone (I do it the other way around - I give commands to the server instead of asking the server to tell me something), but if you can find that, these techniques will work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Looking a bit more for ways to ask the server for its current date/time, you might be able to use exec() and run the date command.  Full disclosure: I have never tested this, nor even thought about it very much!
Dave BaldwinFixer of ProblemsCommented:
Windows servers use 'local time' but Linux servers do not use local time, their clocks are almost always set to GMT (Greenwich Mean Time) or Europe/London which is essentially the same thing.  Software running on Linux is expected to set their timezone to get the correct local time.
you can read the system's timesone from /etc/timezone or possibly $ENV['TZ'] and then use what ray posted or possibly ini_set the timezone or use the default_timezone_set() function so the regular date commands will behave like you want.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.