Partage
  • Partager sur Facebook
  • Partager sur Twitter

Mettre "publié il y a ...." PHP

17 octobre 2020 à 14:53:13

Bonjour à tous !

J'ai un petit problème pour mon code... Enfaite je j'ai une page avec des messages et je voudrais mettre "Publié il y a...minutes/heures ou jours" !

Donc j'ai soustrais mon heure locale moins l'heure du message dans la base de donnée mais ça m'écrit des trucs bizzares! J'avais essayé de me publié un nouveau message pour tester mon code et ça ma marqué publié il y a 57 minutes alors que normalement ça doit me marqué il y 1 minutes...Donc j'éspère que vous pourrez m'aider !

Voici mon code :

$req = $bdd->prepare('SELECT date_annonce, id FROM annonce ORDER BY date_annonce DESC LIMIT 0,1');
   $req->execute();

    while($data_date = $req->fetch())
    {


      $origin = date_create(date("Y-m-d H:i:s"));
 $target = date_create($data_date['date_annonce']);
 $interval = date_diff($origin, $target);

 /*---JOURS---*/
    if($interval->format('%d%') == "1")
    {
      echo "<p>Il y a ".$interval->format('%d%')." jour</p>";
    }elseif($interval->format('%d%') > "1" AND $interval->format('%d%') < "30")
    {
      echo "<p>Il y a ".$interval->format('%d%')." jours</p>";
    }
 /*----MOIS-----*/
     if($interval->format('%m%') >= "1" AND $interval->format('%m%') < "12")
     {
       echo "<p>Il y a ".$interval->format('%m%')." mois</p>";
     }
 /*------ANNEE---*/   
     if($interval->format('%y%') == "1")
     {
       echo "<p>Il y a ".$interval->format('%y%')." an</p>";
     }elseif($interval->format('%y%') > "1")
     {
      echo "<p>Il y a ".$interval->format('%y%')." ans</p>";
     }
  /*-----HEURE----*/
     if($interval->format('%h%') == "1")
     {
       echo "<p>Il y a ".$interval->format('%h%')." heure</p>";
     }elseif($interval->format('%h%') > "1" AND $interval->format('%h%') < "24")
     {
      echo "<p>Il y a ".$interval->format('%h%')." heures</p>";
     }
  /*-----MINUTES----*/
      if($interval->format('%i%') <= "1" AND $interval->format('%i%') < "2")
      {
        echo "<p>Il y a moins de ".$interval->format('%i%')." minutes</p>";
      }elseif($interval->format('%i%') >= "2" AND $interval->format('%i%') < "60")
      {
        echo "<p>Il y a ".$interval->format('%i%')." minutes</p>";
      }
}

Merci à vous !

  • Partager sur Facebook
  • Partager sur Twitter
18 octobre 2020 à 10:34:23

Bonjour,

Doublon

Les doublons nuisent au bon fonctionnement du forum et sont donc interdits. Si vous vous êtes trompé de section, il suffit de signaler votre sujet au staff pour qu'il le déplace au bon endroit.

Je vous invite à continuer la discussion sur l'autre sujet : https://openclassrooms.com/forum/sujet/date-mysql-en-date-php

Je ferme ce sujet. En cas de désaccord, me contacter par MP.

  • Partager sur Facebook
  • Partager sur Twitter

Pas d'aide concernant le code par MP, le forum est là pour ça :)