I am having an issue with "file_put_contents" when i run this code:
file_put_contents("/var/www/html/storage/views/temp", "helllo world");
It saves the file with a time 1hr in the future.
So where dose it get the time?
I have set php.ini date.timezone = "Europe/London" (as per system timezone) and that seems not to help
php.ini date.timezone option specifies which timezone PHP will use internally.
Most Unix/Linux system use UTC as hardware and then set the system clock to a given timezone.
You end-up with three different timezone, but it should be totally transparent.
Your problem may be due by the fact that your system timezone is different than your php timezone.
To check your system timezone, you can do date +%Z
Depending on your need you will either have to change your PHP timezone according to your system timezone, or convert the date of your timestamp to manage your file.
Excepting you've a good reason to do it, I suggest to adjust your php timezone with your system timezone.
file_put_contents gives the file the server time. That means, when looking at the timestamp of the file, the timezone of the server must be taken into account and must eventually be transformed to the timezone of the client.
On my linux machine date outputs: Thu Sep 20 11:23:28 CEST 2012 and thats the timestamp for the file also. When in CST timezone, you have to substract 7 hours from the the timestamp to get the creation time in CST.
You need to handle the timestamp value correctly, when you use it. Examine this:
<?php
$file = __DIR__ . '/test.dat';
file_put_contents($file, "hello world");
print(gmdate('Y-m-d H:i:s T', filectime($file)) . "\n");
print(date('Y-m-d H:i:s T', filectime($file)) . "\n");
Output:
2012-09-20 09:29:17 GMT
2012-09-20 11:29:17 CEST
Depending on the specific date method you use, you can determine the desired output.
Related
I'm using date function $now=date("Y-m-d H:i:s"); in php 5.6.30,
The browser output is string(19) "2017-04-21 02:54:54",That's abnormal.
php.ini set is date.timezone = PRC
Centos 7 system time:
[root#localhost sync]# date
Fri Apr 21 14:53:20 CST 2017
While I was installed the PHP 7.0.16 in the same system, php.ini has the same config, but the date output normal in date function(is 24 digit time).
Why happen this and how to let the date normal working.
This is a timezone issue.
In your php.ini, you have the timezone PRC (China) set, but your system time output gives the time in CST (Amerika).
In other words:
Your PHP code does actually give you the 24-hour format, but in a different timezone, where it is in fact 02:54:54
If you need to get the time in a different timezone (like UTC), you can set it like that:
date_default_timezone_set('UTC');
If you need a different local timezone, you can read about the possible values in the list of supported timezones in the PHP documentation
try this below code for date timezone
<?php
date_default_timezone_set('Asia/Shanghai');
echo date("Y-m-d H:i:s");
?>
I'm add the code to php.ini date.timezone = Asia/Shanghai,
and install chrony and start it to synchronized. It works.
I know this is probably a question for ServerFault but I am having difficulty logging in.
I have an Ubuntu instance in the cloud running Nginx + PHP5-fpm.
I have set the timezone in php.ini to Asia/Singapore and verified it is set in phpinfo().
I have set the OS timezone using dpkg-reconfigure tzdata as well.
For some time, I've been having trouble with wrong dates set in my application. I initially thought this might be something I did in my PHP setup, so in my bootstrap script, I included:
date_default_timezone_set('Asia/Singapore');
Tried installing timezonedb via PECL as suggested in this post:
Setting default timezone does not work despite timezone being valid
A user set date set on a webform still gets translated to "yesterday" when processed. I have tried both date() & gmdate() in PHP with the same results.
Edit
A little more information in case.
User selects a date with jQuery DatePicker
On form submission, I send the timestamp back to the server for PHP to process & store. I divide the timestamp by 1000 in PHP before storing.
<?php $timestamp = (int) $_POST['birthday'] / 1000
// this is received from a form.
Upon echoing the date & timestamp,
<?php echo date('dS F Y', (int) $timestamp);
// when rendering to HTML...
// computes to 13th April 1981
//JS
new Date(data.timestamp * 1e3).toString()
// the exact same timestamp from earlier but received from server.
// computes to Tue Apr 14 1981 23:30:00 GMT+0730 (SGT)
Any ideas?
Your clock is assumed to be in UTC/GMT, but the "humanising"/ conversion to a string adds the time zone offset. The HTTP header being in GMT will be on the original value. This is generally how Unix clocks work, it makes global traffic routing possible.
<?php
# my locale is configured for London
var_dump(time(), date('Y-m-d H:i:s'));
date_default_timezone_set('Asia/Singapore');
var_dump(time(), date('Y-m-d H:i:s')); # MySQL, locale
var_dump(time(), date('r')); # RFC 2822
var_dump(time(), date('c')); # ISO 8601
Your server is reporting the correct time in UTC.
To fix, could you emit that header inside the PHP? This will override the first value...
header("Date: ".date('r', time()+8*60*60));
Edit
As you changed the question, more text response...
I think its necessary to confirm all the date-as-int operations are done with UTC/GMT time.
If your user in in Singapore the time will be sent to the server in +8h offset. Are you transmitting as text or an int? All of the jQuery dates I have used return a string.
If you unpack via strtotime(), it corrects the time offset.
The /1000 should have no computation significance, 8h = (60*60*8)s = 28800s which is >1000.
What does your client say for the timezone ~ gettimezoneoffset
It looks like one of the convert-to-int operations didn't remove the timezone offset.
There was a bug listed and patched in Ubuntu launchpad. timestamps are working after updating PHP. An excerpt of the bug:
[Impact]
A regression of timezone handling between Precise and Quantal means that PHP scripts that depend on the system timezone now use UTC instead. This breaks arbitrary PHP scripts - eg. cactus stops working as expected.
Not affected: 5.3.10-1ubuntu3.4 (Precise)
Affected: 5.4.6-1ubuntu1 (Quantal)
Not affected: 5.4.4-7 (sid)
Workaround: edit /etc/php5/*/php.ini, uncomment the "date.timezone" line and set it to what you need.
[Test Case]
Set a timezone other than UTC using "dpkg reconfigure tzdata".
$ php -r 'echo date_default_timezone_get()."\n";'
Expected results: system timezone (eg. "Europe/London")
Actual results:
PHP Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Command line code on line 1
UTC
(where in this case UTC is the system timezone).
This might be one of the stupidest question but....ok this is my code
date("l, M-d-Y, H:i:s")
but somehow the output shows when I run it through my computer instead of a server
Saturday, Feb-16-2013, 00:21:49
and my computer time is actually
Friday, Feb-15-2013, 16:21:49
and when I uploaded it into a server to try the code this is what it showed
Friday, Feb-15-2013, 19:21:59
Any reason why the date()is few hours ahead and the time is different when I upload to a server.....
I used the code P and e and Timezone identifier shows UTC with +00:00(GMT)
but I believe my GMT should be -08:00 or +08:00 I forgot.
Did I do anything wrong with the codes or just some settings I need to adjust with my computer? Because this happens to both my laptop and desktop.
Thanks in advance.
You should adjust timezone before accessing the date. In php there's a function to set the timezone
date_default_timezone_set("Asia/Calcutta"); //setting timezone
date("l, M-d-Y, H:i:s");
Here's the list of all timezone
http://php.net/manual/en/timezones.php
Check your php.ini for date.timezone:
http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
e.g. date.timezone = "Europe/Berlin"
or you can use
http://www.php.net/manual/en/function.date-default-timezone-set.php
I am trying to convert 1355657846 and 1355677646 unix timestamp to Y-M-D H:i:s format.
The problem is in H:i:s . It should be
11:37:24 and 17:07:26 respectively but it is showing 12:37:24 and 18:07:26.
<?php
echo date('Y-m-d H:i:s','1355657846');//2012-12-16 12:37:26,must be 11:37:26
echo date('Y-m-d H:i:s','1355677646');//2012-12-16 18:07:26,must be 17:07:26
?>
It should be 11:37 and 17:07 because I checked it in unix timestamp conversion
and also it is the time I had received mail in gmail account. And I got these unix timestamp from gmail( using php imap function...$overview->udate)
I am testing this on local xampp server.
Can anyone suggest me where I am going wrong here?
PS: I checked related question in stackoverflow, but here I want to convert timestamp to datetime, which I think should be constant irrespective of timezone.
It is definetily the timezone setting in your local web server.
Check the php.ini for date.timezone value.
It also may be overridden by htaccess file or you PHP script.
strtotime was included once DateTime was created. The parsing is dependant of the server default timezone.
If you want get your date with the good timezone, there are two ways :
default_timezone_set('Europe/Paris');//for example
or :
$date = DateTime::createFromFormat('U',$timestamp,new DateTimezone('Europe/Paris'));
echo $date->format('Y-m-d H:i:s);
you can find more informations here .
This is probably due to the TimeZone setting. Learn about PHP date_default_timezone_set() and make sure your local time zone matches the timezone you really want. All Unix Timestamps are the same world-wide. But the local DATETIME values vary around the globe. This article may be visible to you (hope so):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
Best regards, ~Ray
So the clock is 18:37 right now in sweden but it prints out 16:37 why is that?
$timestamp = time();
date('M d, H:i', $timestamp)
What can be wrong?
Your date.timezone setting in your php.ini file is incorrect. Make sure that it is set to the proper value for your timezone:
date.timezone = Europe/Stockholm
If you do not have access to the php.ini file, you can use date_default_timezone_set() to set it during runtime:
date_default_timezone_set('Europe/Stockholm');
For a list of supported timezones, refer to the PHP Documentation.
If it still doesn't work, make sure your server is set to the proper timezone. If you've set the time manually and the timezone is incorrect (but since the time has been corrected manually it still shows the proper time), PHP has no way to get the UTC time properly and therefore returns the incorrect time.
It is possible that your server is located in a time that is 2 hour back from you.
You can use this page of the documentation to fix the timezone issue.
Try a line like this:
date_default_timezone_set('America/New_York');
Except, y'know, for Sweden.