I'm using the Twitter API to show my latest tweets on my website using PHP.
This is the part where I show the timestamp:
echo '<time data-original="'.$tweets[$i]['created_at'].'" title="'.date('l, j F Y', strtotime($tweets[$i]['created_at'])).' at '.date('g:ia', strtotime($tweets[$i]['created_at'])).'" datetime="' .date('Y-m-d', strtotime($tweets[$i]['created_at'])). 'T' . date('H:i:s', strtotime($tweets[$i]['created_at'])). 'Z" pubdate>"';
What I have noticed is that the dates are incorrect. For example for a tweet that was ACTUALLY posted at 11.51pm the following is parsed by the above code:
<time data-original="Sun May 04 22:51:43 +0000 2014" title="Sunday, 4 May 2014 at 3:51pm" datetime="2014-05-04T15:51:43Z" pubdate=""><a target="_blank" href="#">19 hours ago</a></time>
So first of all the data-original is one hour out (perhaps due to DST, but why hasn't the API handled this? As it knows my location via my timezone settings right?).
The second issue is that due to me formatting the dates in a certain way it seems to be switching the dates to the location of the hosting server so moving them back several hours.
Any ideas on how to fix this?
What I'm trying using JavaScript:
var d = new Date();
var n = d.getTimezoneOffset();
$('time').each(function(){
var time = $(this);
var timeOriginal = time.attr('data-original');
var timeDateTime = time.attr('datetime');
var timeTitle = time.attr('title');
var timeText = time.find('a').html(); // because the text is inside an anchor
timeOriginal = '';
timeDateTime = '';
timeTitle = '';
timeText = '';
});
So using that offset declared at the top of the page, I need to update the times so they become local rather than the server times. Not quite sure how best to approach this.
You do need to account for the timezone being UTC but you can do that without changing the default time zone if you use the DateTime class
<?php
// Twitter time is UTC
$utc = new DateTimeZone('UTC');
foreach ($tweets as $i => $tweet) {
$datetime = new DateTime($tweet['created_at'], $utc);
//test to see the value
echo $datetime->format('Y-md') . 'T' . $datetime->format('H:i:s');
}
"Problem" 1: Tweets are always send to you with the UTC/GMT timestamp.
"Problem" 2: As the manual for strtotime clearly states, it always uses the default timezone to parse date.
So what you need to do is simple, you need to change the timezone to UTC before you use the strtotime function. If you change the timezone for your server all over, there is a change other date calculation will be wrong. You can use the following function to get the correct date/time.
This will display the time always in the same timezone, even if users view the site from different timezones. If you need a per user method, look below the
PHPcode.
<?php
function tweetStrToTime($t) {
//store the current timezone
$ctz = date_default_timezone_get();
//switch to UTC
date_default_timezone_set('UTC');
//use strtotime
$time = strtotime($t);
//change back to the orignal timezone
date_default_timezone_set($ctz);
//return the result from strtotime
return $time;
}
//a copy of your value in $tweets[$i]['created_at']
$tweettime = "Sun May 04 22:51:43 +0000 2014";
//test to see the value
echo date('Y-m-d', tweetStrToTime($tweettime)). 'T' . date('H:i:s', tweetStrToTime($tweettime))
?>
codepad example to see it in action. Keep in mind that codepad already uses UTC, so in this case it would also work with just using strtotime
Javascript solution:
If you want to start working with javascript to handle the timezone for each individual user, you could also try a library like momentjs which has some nice features for handling date/time. this question also has some other answers on handling timezone in javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With