Why date ("c") produce this strange result? - php

$now= date ("c");
I used a local Apache server and the result:
2015-01-12T23:12:00+08:00
Now here is a problem
Now, at my computer the hour is 22:12 not 23:12
Also my time zone is Jakarta which is +7 not +8
So why does the code produce 2015-01-12T23:12:00+08:00
My apache installation got the timezone somewhere. Somewhere wrong. Where?

Try adding date_default_timezone_set("Asia/Jakarta") at the very top of your PHP file. If that works, change your php.ini to always use the correct timezone (date.timezone="Asia/Jakarta"). That way, you'll reflect the correct timezone of the server (in this case your computer).
To see what the time is in another timezone, you could do what N.B. suggested in the comments to this answer:
$dt = new DateTime("now", new DateTimeZone('Europe/Paris')); //If that's the timezone.
echo $dt->format('c');

I think it's server related stuff and you have to reconfigure the timezone of the server. In php.ini check out the value of date.timezone.

Related

PHP Date() does not match timezone set in php.ini

I have a server that needs to use EST time. I have set the date.timezone = America/New_York in my php.ini, and then confirmed this using phpinfo(); I do indeed see date.timezone and default timezone show America/New_York in my phpinfo().
When I try to echo the date() with PHP via the terminal, I get back a time in PST instead of EST (3 hours behind):
php -r "echo date('Y-m-d H:i:s');"
The server time was initially set to America/Los_Angeles, so I updated that and rebooted the machine. Now when I ask the server for the time, I get the time in EST as expected:
date
So now the server timezone AND the php.ini timezone are both set correctly, yet the PHP date() function still gives me a time that is 3 hours behind. I'm echoing this from the terminal so I'm positive it's not anything in my PHP project files themselves.
What could be causing this?

date_default_timezone_set displays incorrect time

I'm using date_default_timezone_set function to set the timezone but when I echo the time it displays wrong time on local server xampp
date_default_timezone_set ('Africa/Cairo');
echo "<div style='direction:ltr'>".date("Y-m-d / g:i A")."</div>";
the time now is 10:50 am but it displays 9:50 am. So any ideas?
Possible issues:
the server's time is not set correctly, test this by confirming that time in the UTC timezone is correct
the timezonedb is outdated and the timezone in question has changed its DST rules in the meantime, update your PHP version and/or your timezonedb via PECL

time() returns different timestamps based on server

I searched now for many hours, it seem's like nobody got that problem before.
I run a script, which writes the current timestamp into a database, on two servers. Both have the same os, software, ... and the same timezone.
Now I found out, that the diff between some timestamps and the current time() is a negative number (yes, the calculation is correct: time() - $older_timestamp)
I dumped time() on both servers, the result: it differs by exactly one hour.
Check it out:
time() on server #1: -1 hour
time() on server #2: correct timestamp
Since time() should be always UTC-based, I can not explain, how this happens.
Does someone have any idea?
Greetings from Aachen
Apparently server one and server two are set to different times. Check BIOS and your servers' OS time.
To add to the merry fest, I give you another option.
The OS' Time-Zone might be set incorrectly. If both clocks show the same time but one server thinks it is in London while the other thinks it is in Aachen, the UTC time returned from the system call will differ by one hour.
If you would however install say NTPD none of this would happen. (usually)
This is likely caused by the timezone being incorrectly set in the php.ini for Apache. Check the php.ini file for date.timezone = and change it to your timezone. A list of PHP timezones can be found here: http://php.net/manual/en/timezones.php
For example:
date.timezone = 'America/Chicago'
You may also be able to use an ini_set if you cannot edit the php.ini file directly.
ini_set('date.timezone', 'America/Chicago');

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.
you can change the ini date time zone and print the date
ini_set('date.timezone', 'America/Los_Angeles');
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.
OR
some hosts give you possibility to edit php.ini
look for php config in cpanel
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().

PHP wrong date/time

PHP date() & time() return incorrect time:
When date.timezone = "Europe/Riga" the time returned by date() was 03-12-2011 08:57:12, but system time was 03-12-2011 01:57:12 (timezone Europe/Riga - correct time at that moment).
When I changed timezone to "Europe/London", the time changed to 03-12-2011 06:57:12 ( actual time 02-12-2011 23:57:12 )
Time returned by date / hwclock --show was correct (03-12-2011 01:57:12 with system timezone set as Riga)
OS: Debian 6.0
I have checked most of the questions regarding similar issues on SO/Google, but they all seem to have wrong timezone specified.
As far as I can tell there is problem between php -> os.
Of course, because the incorrect time offset is always constant I could subtract difference, but it is not a proper solution.
Any ideas will be greatly appreciated.
Reading PHP manual seems that behaviour of date.timezone is affected by settings in php.ini.
There is another way to set the default timezone in all date/time function and it's the date_default_timezone_set. Try to set it with:
date_default_timezone_set('Europe/Riga');
instead of your date.timezone code.
The problem looks similar to what I have seen on one of my servers. Looks to like bug in php 5.3.2-1. Try to run the php script in the bug report and post your results.
The timezone of the system could be wrong. This results in a shift in a time given by the PHP date() function, althought both php date.timezone (in php.ini) and system time of the server are correct.
I had some problem with timezone too. This can be handy for someone.
In my case, Chile Sumer Time CLST returned wrong timzeone offset.
Updating the timezonedb worked for me.
Go to: https://pecl.php.net/package/timezonedb
For Widnows Download the newest version of dll, copy into "ext" directory. Edit the php.ini and put below line:
extension=php_timezonedb.dll
For Linux You can use :
pecl install timezonedb
and in php.ini put:
extension=php_timezonedb.so
This comment for Azerbaijan timezone
I have problem with 'Asia/Baku' on php 7.2 or php 7.1 version.
I solved problem change to 'Europe/Baku'
You have to set date.timezone in php.ini and restart your server
http://php.net/manual/en/timezones.php
PHP time is based on epoch time scale which uses GMT and later UTC. Most people now refer to it as unix time. Because PHP uses unix time, timezones are not used. I believe subtracting the timezone hours in seconds is the correct method to adjust for the differences.

Categories