strftime() function showing incorrect time - php

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

Related

How to setup timezone xampp mysql and apache?

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");

timestamps not honoured in PHP-FPM

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).

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

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 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