Question

I want to adjust the timezone of the twitter api to my local timezone. Right now it displays:

Thu Apr 03 14:34:29 +0000 2014

However, here it is

16:34:29 (GMT+2).

Code:

if(isset($_POST['keyword']))
{
    $url = 'https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword']
           .'&lang=nl&result_type=recent&count=25';
    $tweets = $twitter->get($url);

    foreach($tweets as $tweet)
    {
        foreach ($tweet as $t)
        {
            echo '<div class = "twitterText">';
            echo '<img src = "'.$t->user->profile_image_url.'"/>'.'<br />'
                 .$t->user->name.'<br />'
                 .$t->created_at.'<br /><br />'
                 .'<p class = "description">'.$t->text.'</p>'.'<br />';
            echo '</div>';  
        }
    }
}
Was it helpful?

Solution

I figured it out myself, working code is:

// ...

foreach ($tweet as $t) {
    $date = new DateTime($t->created_at);
    $date->setTimezone(new DateTimeZone('Europe/Amsterdam'));
    $formatted_date = $date->format('H:i, M d');

    echo $formatted_date;
}

OTHER TIPS

Using the Tweet timestamp_ms JSON attribute is so much easier than using created_at.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top