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
Related
I'm fairly new to PHP so forgive me if this is a stupid mistake that I haven't spotted
I've run into a problem where in our current system where we currently used strtotime and it was returning our date an hour ahead than it actually was set. E.g 1:15pm became 2:15pm when I set the timezone to be European rather than GMT.
I read that strotime had this problem but I can't get it to observe a different timezone if I try and set it.
So I tried working with PHPs DateTime instead.
The user enters the time and they select it as 1:15PM however we want to store it as 13:15. So I did this:
$t = DateTime::createFromFormat('h:i A', $venue['startTime']);
$t_24 = $t->format('H:i:s');
Then I try and create my Date object
$d = DateTime::createFromFormat('d-m-Y H:i:s', $venue['startDay'] . ' ' . $t_24);
$d->setTimezone(new DateTimeZone('America/New_York'));
echo ' ' . $d->getTimestamp();
Trying to set the timezone after the object is set because apparently it doesn't work if you add the timezone as the third argument in createFromFormat
My computers time is currently observing European time currently GMT+1 because we're observing daylight savings time in the UK, I select the time set on the through our system as 1:15pm and because I've set the timezone I expect the timestamp outputted equivalent to 7:15am as it's six hours behind European time, however when I convert the timestamp 1500639300 it's still equal to 2:15 PM. Probably done something stupid but can't quite figure out what? Any help would be appreciated :)
Timestamps have no time zone - they are always in UTC time. If you want to save timezone related data use another format! For example save in H:i:s, as you need it.
you can use gmdate() Function for this
<?php $current_time = gmdate('Y-m-d H:i:s'); ?>
http://php.net/manual/en/function.gmdate.php
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");
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");
I can't see my location time with following php function:
echo date("y-m-d h:m:s A");
it's show 12-05-05 08:05:12 AM BUT it's 12-05-05 12:40:12 PM NOW
Can anyoe fix this issue..
Thanks.
Set your preferred timezone in php.ini:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Manila
You can see the list of supported timezones here.
Or you can go other way, use date_default_timezone_set().
Set setlocale function. Here this the description
Fix your server time, or use this:
http://php.net/manual/en/function.date-default-timezone-set.php
You are using m, but it's used to represent month, not minutes.
This is probably what you'd like to do:
echo date("y-m-d h:i:s A");
After that, you're either using the wrong timezone or the servers time is badly setup. If you have access to the server run date in a terminal.
Apart from Blake comment, i suggets try fixing your time zone, add +/- time you require
time , example
$timediff = 10;
$diff= $timediff+ ($timediff* 60 * 60)
I am using Wanp as a testing server. Here is my code:
$dt = time();
$mysql_datetime = strftime("%Y-%m-%d %H:%M:%S %p %z" , $dt);
echo $mysql_datetime;
Here is the output:
2011-04-17 01:36:55 PM Eastern Standard Time
My problem is that, all the information is correct except or the hour, which is 4 hours ahead. The hour (01 PM) is suppose to be (09 AM). I have checked my date and time on my computer, and the time zone is correct. What would cause this to happen, please help.
Chances are this is due to the timezone being set incorrectly. (Although it's odd that the %z is correct, which I'm presuming it is.)
Have you tried calling date_default_timezone_set with the appropriate timezone value?
You have to set your timezone correctly first before anything else.
Use date_default_timezone_set. Here is list of supported timezones.
Could you try using the DateTime class (assuming you have a reasonably recent php)?
Something like:
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s A e');
Alternatively if it still gets the timezone wrong, you could try to specify it:
$dt = new DateTime(null, new DateTimeZone('EST'));
echo $dt->format('Y-m-d H:i:s A e');
Anyway for more debugging information I'd recommend you look at ini_get('date.timezone') which, if incorrect, should be configured properly in your php.ini file.