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.
Related
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.
i am using
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but its always returning date and time form my PC.
Ex.. today is - 15/07/2015
and i changed my PC date to 17/09/2016
so php Date also returning same date...(17/09/2016)
is there any why to get real time and date?
what i have tried
1. simple date function
2. set timezone
3. i have searched on google but no luck yet...
It will always return your PC date as it well should.
Date & time functions use the server's date and time. So if you're running a local server (WAMP, XAMP or whatever) your PC will be the server and therefore it's time will be used.
Setting the timezone should change the time accordingly though.
There is nothing wrong with your code
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but as #Rizier123 said that if you are using XAMPP or WAMP or any other local server it will show the system time only. I would like to suggest you that put your code to an online server or use some online php compiler, then it will surely gonna give you the expected output.
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'm pulling the most recent listened tracks from last.fm and putting them on my website.
Problem is, the times are retrieved in UTC-0 uts format and appear to be an hour out when comparing them to BST times in order to calculate a fuzzy time stamp ("about 5mins ago", "about an hour ago" etc).
Is there any way solve this so the times always match BST/GMT and adjust when entering and leaving daylight saving time?
Here's a snippet of PHP code i'm using at the moment, which results in the times being an hour out.
$now = time(); // use this so all times are to the same second
$tz = getenv("TZ"); // save local setting so we can reset it later
putenv("TZ=Europe/London");
$trackPlayedAt = date('d M Y H:i:s', $track->date->uts);
date() automatically formats to the local timezone. The timezone depends on the configuration of the PHP server. If everything is set correctly, it should just work.
If you are running PHP 5.3 you have more options. Comment with which version of PHP you are running.
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.