Default time zone in php - php

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

Related

PHP date showing wrong time despite the timestamp being correct

I'm having a problem with the PHP date function which I've never had a problem with before.
The timestamp is entirely correct, however for some bizarre reason date() is outputting a time which does not correspond.
I have the following timestamp (and this is definitely the correct one - when I echo it out onto the page, as well as in the database, it shows as being correct):
464400
Yet when I use the following line of code:
<?php echo date("H:i",$timestamp); ?>
I'm getting a time of 4 am? If I paste the timestamp into a timestamp converter website, then it shows the time should in fact be 9am.
I'm completely stuck, this has never happened to me before & this problem has only just come up recently - the code hasn't been changed and everything seemed to be working correctly before.
Does anyone have any experience with this issue? Any help would be appreciated.
That time stamp is is 9am GMT timezone, if you are in another timezone then you will need to adjust it accordingly.
http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('Europe/London');
or even better in your php.ini
http://php.net/manual/en/datetime.configuration.php
date.timezone="Europe/London"
Or you can use more specifically GMT instead of Europe/London (which has DST)
try this method will work
for time zone
http://php.net/manual/en/timezones.php
code
<?php
date_default_timezone_set('Asia/Kolkata');
$dt2=date("Y-m-d H:i:s");
echo $dt2;
?>
try this
// set default timezone
date_default_timezone_set('UTC');
//define unix timestamp
$timestamp = 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
Try this converter too http://freeonlinetools24.com/
For time zone: https://www.php.net/manual/en/timezones.php
date_default_timezone_set('America/Chicago');
echo date("m/d/Y h:i:s A");

PHP Time mismatch between date() and time()

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

Php Date function not working in live server

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

php date function having trouble

Hi guys i am using php date function to show date. But there is a problem with date function of php it is showing a date of yesterday. I am in Dubai so the date of today in my country is
2013-02-23 but php date function showing me date 2013-02-22 please tell me how to correct it.
I am using this date function of php
date("Y-m-d");
You want to set the default time zone to get the right information. Since you said you're in Dubai then you most likely want:
date_default_timezone_set('Asia/Dubai');
Try adding that line to your initialization of the script before the date call and it should be corrected to be for your country.
Reference Manual for PHP on the function and the available time zones:
http://php.net/manual/en/function.date-default-timezone-set.php
http://www.php.net/manual/en/timezones.php
Use date_default_timezone_set to set timezone because server you are using might have different timezone set.
<?php
$timezone = "Asia/Dubai";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
echo date('d-m-Y H:i:s');
?>
list of timezone
Have you checked date & time settings on your machine? The code below works fine on my machine, shoing current date date("y-m-d");

PHP Datetime Error

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']));

Categories