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.
Related
When I write this line of code on local, the output will be this:
echo time(); // 1464605083
And when I write in on the 3v4l (online PHP shell) the output will be this:
echo time(); // 1339412843
As you see there is a huge different between those outputs. What's wrong? And how can I sync them?
Note: I'm using Xampp on my local.
If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set a timezone to use.
The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
That timezone will be vary according to the servers you are using. If you want to use same timezone in all your server. Use
date_default_timezone_set('timezonename')
use timezone i hope it's work ...
<?php
date_default_timezone_set('America/New_York');
echo $date= time() ;
?>
The webserver where you are testing this has a completely off system time set.
If you look at the date of your test script (right next to the title field) you'll see that it has been saved at # Mon Jun 11 2012 11:07:23.
Your timestamp 1339412843 translates to 06/11/2012 # 11:07am (UTC).
So, the server time is just wrong. Test your script someplace else if time is critical.
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
I have tried using php date function() like as follows
$date=date('Y-m-d').' '.date('H:i:s');
echo $date;
the output displayed is 2013-04-03 09:04:02.. but my system is 02:49 pm...
What time is being displayed for me? I tried changing the internet timing even then I am getting the same answer ?
First off, it is not necessary to use the date function twice. This will do the same thing:
echo date('Y-m-d H:i:s');
Second, you need to set PHP's date.timezone. This can be done in the php.ini file, but it can also be done using the date_default_timezone_set function, like this:
date_default_timezone_set('Europe/Amsterdam');
The string that you have to put in can be found in the documentation.
It may also be worth noting that you can tell the date function to use any time. This is done by passing in a *nix timestamp as the second argument. For example:
// One week ago from now
echo date('Y-m-d H:i:s', time()-604800);
It will show server's time only. If possible compare with your server time. If you want to use local machine's time you need to go with JAVASCRIPT.
And another suggestion,
You don't have to use individually to display date & time. You can achieve this in a single statement like this.
$date=date('Y-m-d H:i:s');
You will get the same format 2013-04-03 09:04:02
check for your system timezone and your default timezone in php by opening phpinfo()
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
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.