How to setup timezone xampp mysql and apache? - php

I am using XAMPP - PHP and MYSQL servers. When I tried to use following -
getRates(date('Y-m-d'));
function getRates($cDate)
{
$query = "SELECT * FROM randa WHERE date like '$cDate'" //it only worked at times.
}
?>
Then I realized the date('Y-m-d') does not return the correct date. Went to php.ini and changed time zone. And is still returning the wrong date.
How can I fix this ?
Thank you

Try this
1) In httpd.conf (\xampp\apache\conf\httpd.conf) , add the following line:
SetEnv TZ Europe/Moscow
2) Edit php.ini (\xampp\php\php.ini) date.timezone value in [Date] section:
date.timezone = "Europe/Moscow"
3) In my.ini (\xampp\mysql\bin\my.ini) add or replace
default-time-zone = "Europe/Moscow"
Restart Apache and MySQL

Please add this code on your top page.
date_default_timezone_set('America/Los_Angeles');
Search your country here
http://php.net/manual/en/timezones.php

Solved: but feeling stupid.
my local machine had the correct time however, time zone was incorrect. Changed the time zone and it worked. I don't understand why though. The time WAS CORRECT. Zone was not.

MySQL stores datetime as [yyyy-mm-dd hh:mm:ss] at the UTC timezone. Your field in the db may be set up incorrectly but it may help if you post it's parameters. You shouldn't need to change settings in the conf or php.ini files. I suspect the difference between UTC and your timezone is making the query fail at certain times of the day because the UTC timestamp has passed into the next day. You may need to compensate your date prior to making the query by adding (or subtracting) the right amount of time so the datetime matches UTC.
Something like this:
getRates(date("Y-m-d h:i:sa",(yourtimestamp + (7 * 60 * 60))));
Or declare the timezone prior to the query:
date_default_timezone_set("America/New_York");

Related

time() results a different time

I think it has something to do with PHP Timezones. My current date and time is 10:51 pm , and 7/15/2013 . (I am on windows and my bottom right is showing it :) )
I use the following php code
<?php
echo date("d/m/y : H:i:s", time());
?>
And the browser displayed: 15/07/13 : 19:06:15
(about 3 hours and 45 minutes early).
First Question: Why does it happen?
Second Question: If it is because PHP's default timezone is something else (mine is GMT 5:45), how can i edit the php's conf (or whatever) so that time() returns time for my timezone?
You can either set the time zone in your php.ini file or you can do it in the code:
<?php
date_default_timezone_set('America/Los_Angeles');
?>
http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
http://php.net/manual/en/function.date-default-timezone-set.php
http://www.php.net/manual/en/timezones.php
Also date_default_timezone_get() will show you what timezone you currently have set.
You should look date.timezone at your php.ini
Remember that PHP is server-side. The time it returns is bound to the server the code is running on. The php.ini setting is date.timezone. But you can use date_default_timezone_set to override the timezone specifically for your script. If you want to get the time in the timezone of the client, however, you have to use a client-side method like Javascript.

php strtotime function showing 1 hour more

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

strftime() function showing incorrect time

The strftime() function in php is not showing the correct time on my system. I am running php on xampp. Its around 11 o'clock on my PC clock, but the function strftime() returns 19 when I execute the following code:-
echo 'Time is - '.strftime('%H');
You can change your servers time zone by executing an ini_set at the top:
ini_set( 'date.timezone', 'Europe/Berlin' );
If you're on a hosting account the server is likely in a different time zone than you are. Even if you're running something locally I usually find it's set differently.
http://us3.php.net/timezones
Maybe there is an wrong timezone set in php.ini:
http://www.dedyisn.net/linux/how-to-setting-default-time-zone-in-apache-webserver-for-php/
date("H"); also gives wrong time?
You can also set your default timezone with running the following line on every request. You can achieve this easily if you put it in like a config.php or header.php file of your project.
date_default_timezone_set ( string $timezone_identifier )
Source: http://php.net/manual/en/function.date-default-timezone-set.php
Timezone names: http://www.php.net/manual/en/timezones.php

PHP date behaving incorrect

I don't know what I am missing but PHP's date function is behaving very incorrectly.
The date on my PC shows 26th October 2011, 11:32 AM but when I do:
var_dump(date('Y-m-d H:i:s'));
I get 2011-10-26 06:02:28
What is happening? This is driving me nuts.
You must change your date.timezone in php.ini.
Here's a list of the available timezones for PHP
Don't forget to restart your server after a new timezone is set.
I think it's a problem with the time zone settings
try some thing like that
date_default_timezone_set('Europe/Dublin');

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