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.
Related
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.
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.
if I set my timezone to GMT and use the PHP date() function to get the current time/date stamp it gets the correct time. however when I used a unix generator to make a timestamp for 9am this morning it comes back as 10am even though I've used various different generators and made sure they are in GMT. why is it coming back an hour fast? here is my code:
<?php
date_default_timezone_set('Europe/London');
$timestamp_time = date("hi", 1377766800);
echo $timestamp_time;
?>
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 confuse in the date in javascript and php.
<?php
$mydate = date('2012-05-02 17:00:00');
echo 'Today PHP --'.$mydate;
$mytimstamp = strtotime($mydate);
echo '<br/>My PHP unix time stamp --'.$mytimstamp;
echo '<br/>'; ?>
<script type="text/javascript">
document.write('My Javascript unix time stamp --'+new Date(Number('<?php echo $mytimstamp;?>')*1000));
</script>
OUTPUT
Today PHP -- 2012-05-02 17:00:00
My PHP unix time stamp --1335978000
My Javascript unix time stamp --Wed May 02 2012 22:30:00 GMT+0530 (India Standard Time)
Why i m getting different time in javascript????
You are getting the same time, except in different time zones.
On PHP side you have a GMT time, and on JavaScript side you have India time.
It looks you are from India. Just make sure you understand the concept of time zones and you store the time with time zone information or in GMT/UTC time. This way you should avoid issues with incorrectly using timestamps from different time zones. Displaying such time in the form suited for user's time zone becomes trivial if you know the time zone in which it was generated.
if you do not change your timeZone than change your code like
<script type="text/javascript">
var d = new Date();
var offset = d.getTimezoneOffset();
document.write('My Javascript unix time stamp --'+new Date(Number(<?php echo (intval($mytimstamp));?> + offset*60 )*1000));
</script>
Your PC must have a different timezone set to that of the server running the PHP code.
The PHP date/time is calculated using the setting for the server whereas the JavaScript date/time is calculated using the clients PC settings.
In the javascript is depend on your browser. You would better decode date format on your javascript. You will get another different date on your different browser.