php date function having trouble - php

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

Related

Default time zone in 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

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

Why is DATE_FORMAT() in php a few hours behind?

The form I am using to submit data to the database is processing the time incorrectly (usually a few hours behind the actual time). The output below was really done at 9:28 not 7:28. How do I fix this so that it processes the time correctly? I do not want to use military time. Is it possible that it could be something with my website hosting service? I tried this on XAMPP and everything works fine. Any help would be greatly appreciated.
Code:
DATE_FORMAT(posts.post_date, '%W, %M %e, %Y at %h:%i %p') AS date
Output:
Saturday, July 6, 2013 at 07:28 PM
The time would go by your server time. An easy workaround for this is to manually set the timezone before the date() or time() functions are called to.
I'm in Kolkata, India so I have something like this:
date_default_timezone_set('Asia/Kolkata');
Or another example is LA - US:
date_default_timezone_set('America/Los_Angeles');
You can also see what timezone the server is currently in via:
date_default_timezone_get();
So something like:
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
So the short answer for your question would be:
// Change the line below to your timezone!
date_default_timezone_set('Asia/Kolkata');
$date = date('m/d/Y h:i:s a', time());
Then all the times would be to the timezone you just set :)
As I commented above, you can set the default timezone for your dates and times using
date_default_timezone_set(). The documentation can be found here.
The list of supported timezones can be found here. You can pass your default timezone as a parameter in the aforementioned method.
If you turn E_NOTICE on, you'd probably work this one out as it chucks up notices if you use date/time functions without setting the time zone.
bool date_default_timezone_set ( string $timezone_identifier )
E.G:
<?php
date_default_timezone_set('America/Los_Angeles');
http://www.php.net/manual/en/function.date-default-timezone-set.php

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