I am trying to figure out why php date() is giving me the wrong time, setting the actual time back 2 hours.
<?php echo date("Y-m-d H:i:s"); ?>
This gives 2011-01-01 03:14:04 instead of 2011-01-01 05:14:04. The hour is decreased by 2.
I have not change my timezone for date() and when users visit the site I want the time to be correct for their timezone also. How can I get this to work using php?
it is because by default it shows GMT time you can change for your region with following code
date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time
List of Supported Timezones
http://www.php.net/manual/en/timezones.php
You would have to use either date_default_timezone_set() or a datetime object, and the user would have to set their own timezone in an options menu somewhere.
Otherwise, PHP is a server side language and has no idea what time it is on the user's end.
You would have to use a client side language, JavaScript. You could either have it just be static and display the current user system time, or if for whatever reason you needed to get their time into PHP, you could use some AJAX like scripting to have JavaScript send their time into a script when the page loads.
Try setting the the timezone: date_default_timezone_set or via the ini
Update: you cannot set the correct date for your users. Javascript can handle it but you'd have to rely on the user's system to determine his/her time.
//Change date format
$dateInfo = date_parse_from_format('m-d-Y', $data['post_date']);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year']
);
$data['post_date']=date('Y-m-d',$unixTimestamp);
Related
When I echo
date_default_timezone_get()
It echo's the server datetime instead of my current timezone. I wanted to have my real timezone.
How can I proceed with this.
You can set your default zone with:
date_default_timezone_set('America/Los_Angeles');
and get it:
$your_timezone = date_default_timezone_get();
Here you can check supported timezones.
PS: When you know how to set timezone, you can get users browser timezone, tutorial but you should use JS :)
If i use
<?php
date_default_timezone_set('Asia/Dhaka');
?>
as default timezone to a page of my website.
And if I echo this on this page:
echo date_default_timezone_get()." ".date('Y-m-d H:i:s').
A user from America/Los_Angeles will find the date?
There is nothing built-in to PHP that can detect the client's time zone. Instead, you'll need to detect the user's time zone in JavaScript. You can then send that string to the server, and pass it into PHP's various time zone functions.
How to get correct date not by system date in php
For ex: actual date is 24/4/2013, but I changed my system date is 25/4/2013.
How can get correct date ie 24/4/2013?
PHP can't inherently know that your system time is incorrect. As others have pointed out, you can query an authoritative source (similar to the solution offered here) instead.
EDIT: shortcut to the time server query example
http://tf.nist.gov/tf-cgi/servers.cgi
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Set the default time zone prior to use the date() function.
For instance:
date_default_timezone_set('UTC');
// or
// date_default_timezone_set('Europe/Madrid')
echo date("Y-m-d H:i:s");
Please help me with the php code to get the timezone and the difference in current system time with the London time
I need to get the time zone from where the mail is sent.After that i need to get the current system time as well as the GMT difference of current system time in php
get the current system time zone offset
get the the london time zone offset(from mail is sent)
London time zone : date_default_timezone_set('Europe/London');
PHP is server-side, and calling date() will give the time of the server.
You can pass different arguments to date() to get the information you need. date('Z') will give you the timezone offset in seconds (offset from UTC/GMT). You can also get it in hours and minutes using date('P').
To get the timezone of your local machine, you'll have to use something client-side, like Javascript. You could use jsTimezoneDetect to do this.
Give Javascript access to the server date, and compare the two dates:
var server_date = new Date(<?= date("YYYY-MM-DD") ?>);
var local_date = new Date();
You'll need to replace YYYY-MM-DD with a more appropriate time-and-date format string.
This blog and this PHP class should be enough for your needs
Tested function to get timezone difference between UTC and User's Timezone (Asia/Kolkata) can found
TimeZone Difference
I am using date() function it getting the date and time as per my given format but the time its showing me is 4 hours forward than my current local machine time:
This is my code
echo date("Y-m-d h:i:s", time());
Its showing me : 2009-10-28 08:47:42
Where as it should Disply : 2009-10-28 04:47:42
Any Idea whats wrong with this and why its showing different time.
it is likely giving you GMT, you need to set your timezone: e.g. date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Make sure your time zone is set correctly:
e.g.
date_default_timezone_set('UTC');
It's returning the timezone of your server, not your computer
try http://www.php.net/manual/en/function.date-default-timezone-set.php
Because you are in, probably, US/Eastern (America/New_York, currently EDT) time zone, but the PHP you are using is running in UTC. You need to ensure that the TZ variable is set in the PHP environment.