Problem in DATE AND TIME - php

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.

Related

PHP is giving unexpected results from epoch time conversion

I need to convert an epoch time to standard Hour and Minutes. Using various examples found on Stackoverflow, I have been unable to get the correct return. I am using the following
$seconds= 1495587600;
$mytime= date("H:i", $seconds);
echo $mytime."<br>";
I have checked the value of $seconds on a time converter site and get the correct result. I would expect the return from the code to be 09:00, instead I get 01:00. Is there something I am leaving out?
This looks like a time zone issue. PHP date() works using the local time zone which can be set with date_default_timezone_set(). Your hosting company or some other server configuration may be setting a different default time zone than what you expect.

Issue with getting current time and current timezone in WordPress

I have included a php file in my wordpress website. In the php file, I am getting the current time (in my current time zone) like this:
$time = date('Y-m-d H:i:s');
var_dump($time); die(date_default_timezone_get());
The date comes correct however the time is wrong and the time zone comes as UTC which is also wrong. Here is the result of var_dump():
string(19) "2015-09-21 01:32:24" UTC
I have checked all of the posts in stackoverflow regarding getting current time. Some developers have suggested below function if you are using WordPress which I tried and it is still giving the wrong time:
$time= current_time('timestamp', true );
I appreciate any guidance or solution.
Before your other code, set your desired time zone using
date_default_timezone_set ('America/Toronto');
See the list of supported time zones.

How to get correct date not by system date in php

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

php code to get the timezone and the difference in current system time with GMT

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

PHP date() function not giving correct time

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

Categories