I have a pubDate in xml which I have to write to mysql database - everything works fine but I have a problem with the time zone:
date_default_timezone_set('Europe/Warsaw');
$date = date("Y-m-d H:i:s", strtotime($entry->pubDate));
Data in xml file:
<pubDate>Sat, 02 Sep 2017 20:41:30 +0000</pubDate>
Score in the database:
2017-09-02 22:41:30
As you can see the difference is two hours but I do not know what it is and how to get the correct effect.
What is your database timezone ? try this request SELECT ##global.time_zone, ##session.time_zone; Maybe you also need to change your mysql timezone
You have to set the timezone for mysql too.
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.
Background: I have some PHP code that calculates the next time a recurring message should be sent according to various rules. I also have a sanity page that double checks that the next recurrence is correct. Due to a timezone problem that I am not sure how to resolve, the sanity page is showing some problems that I am not sure if they are actually problems.
I believe that these are showing up because the (future) date is during BST (British Summertime) but the Europe/London timezone is currently on GMT.
I created a very simplified test page to debug and test one example. The code is as follows:
// Not included: DB connection setup, which includes a query to set the MySQL session timezone to the current timezone for Europe/London as calculated by PHP
// Copied from a comment at http://php.net/manual/en/function.date-default-timezone-get.php
function timezone_offset_string( $offset )
{
return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) );
}
$offset = timezone_offset_get( new DateTimeZone( date_default_timezone_get() ), new DateTime() );
// query_multiple() is a function to get a single result from the database and return it as the specified PDO type
$db = query_multiple("SELECT NextRecurrence, UNIX_TIMESTAMP(NextRecurrence) AS NextTS, IF(##session.time_zone = 'SYSTEM', ##system_time_zone, ##session.time_zone) AS DBTZ FROM RecurringMessages WHERE RecurID=96 LIMIT 1", "", "", PDO::FETCH_ASSOC);
print "DB Date of NextRecurrence : ".$db['NextRecurrence']."<br>";
print "DB timestamp of NextRecurrence : ".$db['NextTS']."<br>";
print "DB timezone : ".$db['DBTZ']."<br>";
print "PHP timezone and offset: " .date_default_timezone_get().", ".timezone_offset_string( $offset ) . "<br>";
print "PHP date of NextTS : ".date('Y-m-d H:i:s', $db['NextTS'])."<br>";
The output of this page is as follows:
DB Date of NextRecurrence : 2017-05-24 10:00:00
DB timestamp of NextRecurrence : 1495620000
DB timezone : +00:00
PHP timezone and offset: Europe/London, +00:00
PHP date of NextTS : 2017-05-24 11:00:00
As you can see, the PHP date is one hour different, even though the PHP and MySQL timezones are the same. As mentioned above, I believe that this is because the date given is during BST and Europe/London is currently GMT.
If I put 1495620000 into http://www.epochconverter.com/, it tells me that it is Wed, 24 May 2017 10:00:00 GMT
There are two things that I am currently unsure of
Is the current database entry correct? When it comes to May 24th 2017, will this message be sent at 10:00 or 11:00? The PHP code, which is used for sending the message, sets the session time zone for MySQL right after connecting to match the current timezone for Europe/London.
If the database entry is correct, how do I get the PHP date function to give me the correct output for any future date? If the PHP date function is correct, how do I (generically) update the MySQL record with the correct date? All I know is that it should be sent on May 24th 2017 at 10:00. Currently the query updates with UPDATE SET NextRecurrence='2017-05-24 10:00:00' ...
I figured it out (I think).
Because MySQL is currently at GMT, I need to present the Y-m-d H:i:s formatted date and time to MySQL in GMT, even if the date is during BST.
// $ts contains the correct unix timestamp
$offsetnow=date('Z');
$tsoffset=date('Z', $ts);
$diff=$offsetnow-$tsoffset;
// Add the difference to the timestamp so that the PHP date returns the appropriately presented Y-m-d H:i:s to MySQL, which as of writing this is GMT.
$ts+=$diff;
$query="UPDATE Recurrences SET NextRecurrence='".date('Y-m-d H:i:s', $ts)."' ...
If anyone sees a mistake in what I am doing, please let me know!
I'm trying to debug a php date issue I'm having between 2 servers. I'm passing a timestamp through the date() function on both servers, but I'm getting 2 different dates.
Server 1:
date('d-m-Y', 575766000);
// Outputs 30-03-1988 00:00:00
Server 2:
date('d-m-Y', 575766000);
// Outputs 31-03-1988 00:00:00
I've checked the date on both servers, with the "date" command, and they're both set to:
Wed Mar 6 14:42:19 GMT 2013
Any ideas?
You can check the timezone by logging on to the server and looking at /etc/php.ini depending on how your PHP is configured.
date.timezone="America/New_York"
As of PHP 5.3 you are required to set the timezone. Previously this could be left blank.
You have to correct the times of your servers.
PHP takes the time from the server.
if you will try to set the timezone using this:
date_default_timezone_set("America/Chicago");
Then it should work
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 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.