NextScripts
NextScripts

Reputation: 31

PHP: How to read the current date/time from the server, not from php.ini

Here is my problem:

echo date('Y-m-d H:i:s'); 
echo date('Y-m-d H:i:s', mktime());  

echo exec('date');

The output is:

2012-03-21 08:45:51
2012-03-21 08:45:51

Wed Mar 21 10:45:51 EDT 2012

Server time is off 2 hours from the time returned by php date(); or any other php date/time function. It happens because server time set to EST and PHP.INI date.timezone="America/Denver"

I need to synchronize those two, by using date_default_timezone_set, but I don't know in advance what is the difference.

Is there any other way to get local server time besides calling exec?

UPD: I know that php.ini setting is wrong and that I can change it. The problem is that this script will work on nobody knows what kind of servers. I can't go to each and every one of them and correct the php.ini file. I also don't know in advance what timezone will be on those servers. I need a dynamic solution that will work everywhere.

Upvotes: 3

Views: 15365

Answers (4)

Md. Tauhidul Islam
Md. Tauhidul Islam

Reputation: 19

Change value of date.timezone from php.ini [Date] and restart your server.
You can get your date.timezone value form-
http://au.php.net/manual/en/timezones.php


For Bangladesh I set in my php.ini [Date]
date.timezone = Asia/Dhaka


You get your php.ini in C:\xampp\php address for XAMP server and Windows.

Upvotes: 2

cmbuckley
cmbuckley

Reputation: 42537

On *nix, you can use formatting parameters to date to get what you need:

  • date +%z — timezone (hhmm)
  • date +%:z — timezone (hh:mm)
  • date +%Z — timezone abbreviation (e.g. "EDT")

Making a system call (e.g. echo exec('date +%z');) will bypass any INI settings as per date_default_timezone_get. Note that this function issues an E_WARNING if it reads from the system time, and indeed from PHP 5.4 it doesn't even allow reading from it — specifically because it can't be relied upon.

To be consistent, regardless of the server settings you should use UTC within your application. For ease of use, GMT is close enough to UTC for most purposes, so you can use gmdate().

Upvotes: 0

zod
zod

Reputation: 12437

you can change the ini date time zone and print the date

ini_set('date.timezone', 'America/Los_Angeles');

Upvotes: 2

qpaycm
qpaycm

Reputation: 930

OR

some hosts give you possibility to edit php.ini
look for php config in cpanel

Upvotes: 0

Related Questions