My date is ahead 1 hour when I post to mySQL via php. I'm in Los Angeles. How can i make the time correct?
Here is what I currently have:
$date = date("m/d/y g:i A") ;
Use this to get the time zone PHP is using date_default_timezone_get();
If you're in Los Angeles you can set your time zone temporarily for only the current script
date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Also pass T in the date function:
echo date("D M j G:i:s T Y"); outputs Mon May 25 16:23:49 EDT 2009
see
date_default_timezone_set
like
date_default_timezone_set('America/Los_Angeles');
First check if servers time is correct ( on linux use date shell command). If yes, you just set the time and you're good.
If not you have to change it in php with setlocale() or date_default_timezone_set() function
It depends on your specific case, but I had saved the date as UTC / GMT time, so I had to use the gmdate function instead of date. E.g.
gmdate("H:i",$time)
Related
This is the coding and it is echoing with the right format but the fact that the date is wrong when it prints out.
Output: 31 Dec 1969 19:33
Database Timestamp 2016-05-20 21:53:17
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i',$row['timestamp']);
?>
and i have tried doing the date in different ways and still the same result
In the code sample you posted, $row['timestamp'] has not been set, so the date is constructed with timestamp 0, also known as epoch, or the date that is being echoed.
If you change it as follows, it should work fine:
<?php
date_default_timezone_set('ECT');
$timestamp = 1456778973;
echo date('d M Y H:i', $timestamp); ?>
Side note:
Time zone ECT is not a valid time zone code in PHP. If I assume correctly that you mean european central time, you would have to specify CET instead.
ECT doesn't exist as a valid TimeZone, did you mean CET perhaps?
The correct way to do this is using the DateTime class, i.e.:
$date = new DateTime();
$date->setTimestamp(1456778973);
$tz = new DateTimeZone("America/Denver");
$date->setTimezone($tz);
echo $date->format('d M Y H:i');
PHPFiddle Demo
Note:
Dates should always be stored in DB as UTC (timestamp aka unix time), then you can add or subtract the timezone offset using the DateTime class.
Would you know what the offset would be for mountain standard time?
Mountain Time: America/Denver
Mountain Time (no DST): America/Phoenix
List of Supported Timezones
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
I have the following code
date("D j M g a", strtotime($start))." - " . date("ga", strtotime($finish));
`["Start"]=>
string(21) "/Date(1354323600000)/"
["End"]=>
string(21) "/Date(1354326300000)/"
}`
The above code outputs Thu 1 Jan 12 pm - 12pm" string(22) "Thu 1 Jan 12 pm - 12pm in the correct format that I want but how could I get the day date and time correct.
Pacific/Auckland is my timezone
The issue is that what you pass to the strtotime - /Date(1354323600000)/ string is not in a valid format for strtotime function.
And actually it's a regular unix timestamp in milliseconds.
So the obvious solution - is to clean up everything but numbers and divide it by 1000:
$unix_time_with_ms = preg_replace('~\D~', '', $date);
echo date('Y-m-d H:i:s', $unix_time_with_ms / 1000);
Prefer setting default timezone in your php.ini file with a line like this:
date.timezone = "Pacific/Auckland"
If for some reason you can't edit php.ini then on top of your PHP script you can set a timezone (effective for that script only):
date_default_timezone_set('Pacific/Auckland');
You can set time zone using
date_default_timezone_set('America/Los_Angeles');
More Link
if you want to set in the script try
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
date_default_timezone_set('America/Los_Angeles');
but if you want to use the same time zone than setting in php.ini will be better solution
date.timezone = "America/Los_Angeles"
I've an application,say the end user might be from any country but when he does some action,i want the date to be shown in a particular time zone.
I want insert this into DB so i'm doing this using date_default_timezone_set(''); with date()
Is this the right way or should i use gmdate() and add time zone.
Thanks
No, don't use date_default_timezone_set() for timezone conversions. This can have unintended side-effects.
Instead, use this:
$tz = new DateTimeZone('America/Los_Angeles');
$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimeZone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";
Note, you're creating the DateTime object using UTC time and then applying the timezone. This way is much cleaner.
If you want GMT, I think you should use the right Timezone as Europe/London
date_default_timezone_set('Europe/London');
I have a simple date time in the format 10/20 4:30PM. I want it to display in the format
10/24 1:30PM -007. I am saying date('Y-m-d H:i:sT', $time_recieved) assuming T is for timezone. But It is still not giving the time zone. What might be the reason? Is there any other format I am missing?
What you want is O not T I believe.
Check the PHP date page for all the format listings possible.
http://php.net/manual/en/function.date.php
You can have a look at the official manual of PHP : date ();
http://www.php.net/manual/en/function.date.php
I tested that and I used on my server with :
$time = time();
echo date('Y-m-d H:i:s T', $time);
it shows the timezone perfectly. Check $time_received, it might be an erroneous number, try with local time with time´() function to ensure that it's OK.
Good luck