php strtotime function showing 1 hour more - 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

Related

Manage PHP time function

I am using godaddy hosting service and I can manage local time, I have to use the server default time that is America/Phoenix.
Even if i am using date_default_timezone_set("Asia/Kolkata");
function in my config file then also there is no difference in time and godaddy people are not ready to help me with, I am tired of calling this guys but no response, I hate them all.
Is there any means I can get my local time using any function or any external API?
I am using this code
//set time zone india
date_default_timezone_set("Asia/Kolkata");
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
$date = date('m/d/Y h:i:s a', time());
echo $date."<br>";
and the output that I am getting is
The current server timezone is: Asia/Kolkata05/14/2014 12:06:26 am
Even if it is 4:38 pm here...
date_default_timezone_set should work for PHP functions like date, however I'm going to hazard a guess that you're having problems with date/time elements in a database such as MySQL.
I know I've had similar problems before, when trying to get everything on UTC instead of Europe/London...
When you've established the connection to your database, be sure to run this query:
SET time_zone = 'Asia/Kolkata';
This, in addition to date_default_timezone_set, should solve your problems. However, if you're using DATETIME columns, then those will not be fixed. TIMESTAMP columns will be automagically fixed to the new timezone because they are saved as UTC internally and converted upon retrieval.
You shouldn't touch date_default_timezone_set. The proper way to do that would be to use DateTimeZone object. Something like this:
$now = new \DateTime('now', new \DateTimeZone('Asia/Kolkata'));
echo $now->format(\DateTime::ATOM);
So, the idea is that you create an object in a timezone of server and then convert it to your timezone

PHP gmdate() returning UTC time an hour behind

So I have a problem whereby when I use the php gmdate() function on my local machine web server it returns the correct UTC time but when I upload the same script to a vps server the function returns a UTC time that is about and hour behind.I am using the UTC time together with javascript to display local time to different clients.
This is how i have called the function:
gmdate('m/d/Y H:i:s', time());
Any help would be appreciated.
It could be the server's timezone or even the default PHP timezone. You can override this using the following function date_default_timezone_set()
date_default_timezone_set('America/New_York');
http://php.net/manual/en/function.date-default-timezone-set.php
You can find a list of supported timezone identifiers here: http://www.php.net/manual/en/timezones.php
Set your date_default_timezone_set() using PHP
Eg:
<?php
date_default_timezone_set("Asia/Bangkok"); // use your local timezone here
echo date_default_timezone_get();
?>
for more click here: http://php.net/manual/en/function.date-default-timezone-set.php

date() not the same as what's showing at my taskbar

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

file_put_contents create file with wrong timestamp

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.

Php clock 2 hours back

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.

Categories