I am having an issue with php date() function.
When I save the date in mysql datebase the hours shows 4 hours less than my current time.
My php code is below: $add_date = date("Y-m-d H:i:s");
It saves the time in database as
2011-08-03 07:51:26
But is should show
2011-08-03 13:22:26
Can anybody tell me how to fix it
Thanks
You have to set a valid time zone - it seems you don't have an appropriate time zone set up in php environment for your location.
Check out
http://www.php.net/manual/en/function.date-default-timezone-set.php
It should give you a clue how to set up the correct time zone in php.
#Mujahid If the printed time and the saved time are the same it probably means your server is not in the same timezone as you. With that said, you have to manually set the default timezone for your PHP script at the top of the file, or get the DateTime by defining your timezone explicitly. Here's the code:
$dateTime = new DateTime("now", new DateTimeZone('America/Los_Angeles'));
$add_date = $dateTime->format("Y-m-d H:i:s");
echo $add_date;
Here's a list of time zones that PHP supports, just find yours and replace America/Los_Angeles with it.
http://php.net/manual/en/timezones.php
Here's a good tutorial on PHP DateTime and DateTimeZone...
http://ditio.net/2008/06/03/php-datetime-and-datetimezone-tutorial/
Hope this helps. Good luck.
Make the time zone setting of the MySQL server and the web server the same.
this will add hosted server time not local time
$add_date = date("Y-m-d H:i:s");
use strtotime() to add hours and minutes
$newdate = date("Y-m-d H:i:s",strtotime('+ 5 hours 30 minutes'.$row['db_date']));
Related
I am trying to save the current date and time and i have used the following code
Current PHP version: 5.4.16
date_default_timezone_set('Asia/Kolkata');
$datet = date("Y-m-d H:i:s ");
When i echo it the output will be
2017-04-25 05:07:17
But actual time what i need is
2017-04-25 10:45:17
I have tried using now() but its also showing the same date rather than the current time, In what ways i can get the Current date and time. Any help appreciated.
have you tried to change the server time, print date on console, if this mismatched, then change the server time.
Check This
This gives you correct time after setting default time zone "Asia/Kolkata"
date_default_timezone_set("Asia/Kolkata");
echo date("Y-m-d h:i:s");
Please check my attachment. Here, First of all I have check
echo date_default_timezone_get();` and getting my default timezone Asia/Calcutta and after that when I changed timezone as
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();
Asia/Bangkok
enter image description here
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
I want to insert user entry log in a database table. The column where I want to keep the current date time is "date_time decimal(10,0) NOT NULL DEFAULT '0'". When inserting data I set the field as
$this->mytable->date_time = time();
My query executed successfully. But when I want to display the time of the entry it shows the time which is not match my pc(local server) time. To display the time I write
echo date('Y-m-d h:i:s A', $log->date_time);
I test several times but it showing the time which is 4 hours less than the exact time. On my test the current time is 2013-09-15 04:46:34 PM but table row shows 2013-09-15 12:46:34 PM.
Please help me. I can not find out the mistake.
You need to specify the timezone. The time() function will just retern a timestamp which is timezone-independent.
When you use the date() function you are using the server's timezone, I would recommend using the DateTime object:
$timezone = new DateTimeZone("Etc/GMT-4");
$date = new DateTime("#".$log->date_time); // #-symbol indicates timestamp input
$date->setTimezone($timezone);
echo $date->format("r");
Here is a list of supported timezones http://php.net/manual/en/timezones.php
Sorry. It was my mistake. When inserting data I set the time zone as
if(function_exists('date_default_timezone_set')) date_default_timezone_set("Asia/Dhaka");
But when display the data I forgot to set the time zone. It working fine when I set the time zone as I defined before in my display page. Thanks everybody for your help.
Try this
<?php
date_default_timezone_get();
echo time();
Manual
i am displaying the current date & time using php in localhost using below date function
<?php
echo date('Y-m-d H:i:s');
?>
and displaying output as 2013-06-24 14:45:13
But when i run the above php code in the live server it is displaying output as 2013-06-24 06:47:28.
So, any one tell how could display the date and time in 24 hour format in the live server..
This is a timezone issue.Check
date_default_timezone_get();
Using date_default_timezone_set('YOUR_TIMEZONE'); you can set timezone.
Seems a timezone issue or settings not properly set on server as theoretically it should display time and date as of server
Regarding Timezone issue, see this guide for date_default_timezone_set
http://php.net/manual/en/function.date-default-timezone-set.php
Your server seems not to have the same timezone as your localhost.
Or maybe the time is simply not well set.
The date function takes the current date and time. Now it will display only the date and time will be displayed that is at server.
Use DateTime:
<?php
$date = new DateTime('now');
echo $date->format("Y-m-d H:i:s");
?>
Also, the date/time displayed is server's one, so you should also set the timezone:
<?php
$timezone = DateTimezone('{Continent}/{City}');
$date = new DateTime('now', $timezone);
echo $date->format("Y-m-d H:i:s");
?>
When you are running this code echo date('Y-m-d H:i:s'); in localhost it is taking your system time but when you run this echo date('Y-m-d H:i:s'); on live server it uses the SERVER Timezone. Use this function date_default_timezone_get(). It returns the default timezone used by all date/time functions in the script and you can use it to set the time zone.
You can
date_default_timezone_set('America/Los_Angeles');
or
date_default_timezone_set('Egypt/Cairo');
or what ever between ('')
Use date_default_timezone_set to sent corrent time zone from your script.
You can find more info here: http://php.net/manual/en/function.date-default-timezone-set.php
I have changed the way i save the time when a log is submitted to the DB by using the 'datetime' stamp in MySQL.
Im saving it in this format date("Y-m-d G:i:s"); which outputs it like so: 2012-02-22 20:20:01
The only problem is im in the UK, and my hosting/server isnt.
I have found out how to edit the time to bring it inline with UK time by doing this:
date = date("Y-m-d G:i:s", strtotime("+14"));
So now i have the time right i thought i would be ok, but now i have noticed that the date is not inline with UK date/time.
If i did a submission now it would display: 2012-02-22 20:25:36 when the time/date is actually 2012-02-23 20:20:01.
So it looks like that because my script is 14 hours behind, its knocking the date out to.
Is there a way i can fix it ?
Thanks, Sam!
include this in your program
date_default_timezone_set('Europe/London');
Add this one line in your php.ini file:
date.timezone = "Europe/London"
Now, anytime you use date or time functions, it will be adjusted to what you have specified.
For a complete list of timezones in Europe:
http://www.php.net/manual/en/timezones.europe.php
Utilizing the PHP Datetime module: Supported Timezones
$tz = new DateTimeZone('Pacific/Kiritimati'); // Change this to YOUR needed Timezone
$datetime_GMT = new DateTime(strtotime($server_or_SQL_date));
$datetime_GMT->setTimezone($tz);
$datetime_GMT->format("Y-m-d G:i:s");
echo $datetime_GMT;