php – Change Twitter date format in a tweet-ThrowExceptions

Exception or error:

So I made a block of code using php to extract the latest tweet from twitter and this works but the date is shown as:

Fri Feb 21 09:24:06 +0000 2020

Is there a way to make this show as:

21 Feb 2020 9:24:06

I used the folowing block of code to load the data into a .txt file and I later retrieve all the data from this file:

$settings = array(
    'oauth_access_token' => "**************",
    'oauth_access_token_secret' => "**************",
    'consumer_key' => "**************",
    'consumer_secret' => "**************"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user'])) 
{
    $user = $_GET['user'];
} 
else 
{
    $user = "User";
}
if (isset($_GET['count'])) 
{
    $count = $_GET['count'];
} 
else 
{
    $count = 1;
}
$getfield = "?screen_name=$user&count=$count&tweet_mode=extended";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if(array_key_exists("errors", $string)) 
{
    echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();
}
$fp = fopen('twitterData.txt', 'w');
foreach($string as $items)
{
    $naam = $items['user']['name'];
    $snaam = $items['user']['screen_name'];
    $date = $items['created_at'];
    $tekst = $items['full_text'];
    $newTekst = str_replace(array("\n", "\r"), '', $tekst);
    $Tweetstring = "<div class='Tweet'><div class='TweetUsername'><h2>". $naam."</h2></div><div class='TweetScreenname'><span>". $snaam."</span><br><span>".$date."</span></div><div class='TweetTxt'>". $newTekst."</div></div></a>";
    fwrite($fp, 'let Tweet = "'.$Tweetstring.'";');
}
fclose($fp);

I just hope someone has the answer for me and it doesn’t have to be specifically PHP it can also be javascript or jQuery.

How to solve:

Nevermind the question i’ve already found the answer which goes as following:

$fp = fopen('twitterData.txt', 'w');
foreach($string as $items)
{
    $naam = $items['user']['name'];
    $snaam = $items['user']['screen_name'];
    $date = strtotime($items['created_at']) - 3600;
    $newDate = date('l d-m-Y h:i:s', $date);
    if (strpos($newDate, 'Monday') !== false) {
        $newDate2 = str_replace("Monday","ma", $newDate);
    }
    if (strpos($newDate, 'Tuesday') !== false) {
        $newDate2 = str_replace("Tuesday","di", $newDate);
    }
    if (strpos($newDate, 'Wednesday') !== false) {
        $newDate2 = str_replace("Wednesday","wo", $newDate);
    }
    if (strpos($newDate, 'Thursday') !== false) {
        $newDate2 = str_replace("Thursday","do", $newDate);
    }
    if (strpos($newDate, 'Friday') !== false) {
        $newDate2 = str_replace("Friday","vr", $newDate);
    }
    if (strpos($newDate, 'Saturday') !== false) {
        $newDate2 = str_replace("Saturday","za", $newDate);
    }
    if (strpos($newDate, 'Sunday') !== false) {
        $newDate2 = str_replace("Sunday","zo", $newDate);
    }
    $newDateFinal = substr($newDate2, 0, 19);
    $tekst = $items['full_text'];
    $newTekst = str_replace(array("\n", "\r"), '', $tekst);
    $Tweetstring = "<div class='Tweet'><div class='TweetUsername'><h2>". $naam."</h2></div><div class='TweetNaam'><span>". $snaam."</span><br><span>".$newDateFinal."</span></div><div class='TweetTxt'>". $newTekst."</div></div></a>";
    fwrite($fp, 'let Tweet = "'.$Tweetstring.'";');
}
fclose($fp);

which gives the output of:

vr 21-02-2020 09:24

and that was exactly what I wanted

Leave a Reply

Your email address will not be published. Required fields are marked *