0

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.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Kevin S
  • 11
  • 4
  • Why was this tagged with Javascript and jQuery? – Rory McCrossan Mar 17 '20 at 09:28
  • it doesn't have to be specifically PHP it can also be solved using javascript or jQuery. Whatever works best. – Kevin S Mar 17 '20 at 09:34
  • Best would be PHP, however if a JS solution is acceptable please include the relevant JS code in the question so we can see the context you're working in. That being said, there is already *a lot* of resources about date formatting in [JS](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) and [PHP](https://stackoverflow.com/questions/2487921/convert-a-date-format-in-php) – Rory McCrossan Mar 17 '20 at 09:39
  • Okay I'm sorry for that I'm new to this, but I've already tried a bunch of different methods but none of them worked. – Kevin S Mar 17 '20 at 09:42

1 Answers1

0

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

Kevin S
  • 11
  • 4