PHP

Comment convertir une date en format français sans la fonction obsolète strftime en PHP :
Voici comment j’ai transformé une date du format US en format français :
$dateUSfull = date(‘l d F Y’);
$dateFRday = str_replace([‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’],[‘Lundi’, ‘Mardi’, ‘Mercredi’, ‘Jeudi’, ‘Vendredi’, ‘Samedi’, ‘Dimanche’], $dateUSfull);
$dateFRfull = str_replace([‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’], [‘Janvier’, ‘Février’, ‘Mars’, ‘Avril’, ‘Mai’, ‘Juin’, ‘Juillet’, ‘Août’, ‘Septembre’, ‘Octobre’, ‘Novembre’, ‘Décembre’], $dateFRday);
echo $dateFRfull;
Résultat : Tuesday 15 August 2023 => Mardi 15 Août 2023

Comment rendre une page d’une navigation ‘active’ en PHP :
Voici comment j’ai rendu une page d’une navigation active :
<ul class=”menu”>
<?php $page = $_SERVER[‘PHP_SELF’]; ?>
<li class=”<?php if (basename($page) == “index.php”) {echo ‘bold’;} ?>”><a class=”link_pages” href=”index.php”>Accueil</a></li>
idem autres <li>
idem autres <li>
idem autres <li>
</u>

Pagination avec un array :
Voici comment j’ai créé une pagination en PHP avec une requête SQL :
<?php
try {
     require “./constants/pdo.php”;

$nbmatch = $statement->fetchAll(); $statement = $pdo->query(‘SELECT * FROM matchs WHERE match_status = “terminé” ORDER BY match_date ASC, start_time  ASC’, PDO::FETCH_ASSOC);

$nbmatch = $statement->fetchAll();
$count = count($nbmatch);
$itemsPerPage = 10;
$nbPages = ceil($count / $itemsPerPage);

if ($count < 10) {
foreach ($pdo->query(‘SELECT * FROM matchs WHERE match_status = “terminé” ORDER BY match_date DESC,        start_time DESC’, PDO::FETCH_ASSOC) as $match_name) {

VOTRE AFFICHAGE
}
} else {
if (isset($_GET[“page”])) 
$currentPage = $_GET[“page”];
$limitX = ($currentPage * $itemsPerPage) – 10;
$limitY = $itemsPerPage – 1;

foreach ($pdo->query(‘SELECT * FROM matchs WHERE match_status = “terminé” ORDER BY match_date DESC, start_time DESC LIMIT ‘ . $limitX . ‘, ‘ . $limitY . ”, PDO::FETCH_ASSOC) as $match_name) 

VOTRE AFFICHAGE 
} ?>
<ul class=”pages_li”> 
<li>
Page <a class=”pages_liens <?php if (!isset($currentPage)) {echo ‘bold’;} ?>”  href=”matchs_over.php”>1 &nbsp</a>
</li>

<?php
for ($i = 2; $i <= $nbPages; $i++) { ?>
<li class=”<?php if (htmlentities($currentPage) == $i) {echo ‘bold’;} ?>”>
<a class=”pages_liens” href=’matchs_over.php?page=<?php echo $i; ?>’><?php echo $i; ?>  &nbsp</a>
</li>
<?php } ?>
<li>&nbsp &nbsp &nbsp &nbsp &nbsp</li> <?php
echo ‘</ul>’;
echo ‘<br>’;

} else {
foreach ($pdo->query(‘SELECT * FROM matchs WHERE match_status = “terminé” ORDER BY match_date     DESC, start_time DESC LIMIT 0, 10’, PDO::FETCH_ASSOC) as $match_name) {

VOTRE AFFICHAGE
 } ?>
<ul class=”pages_li”>
 <li>
Page <a class=”pages_liens <?php if (!isset($currentPage)) {echo ‘bold’;} ?>”  href=”matchs_over.php”>1 &nbsp</a>
</li>

<?php
  for ($i = 2; $i <= $nbPages; $i++) { ?>
<li class=”<?php if (htmlentities($currentPage) == $i) {echo ‘bold’;} ?>”>
<a class=”pages_liens” href=’matchs_over.php?page=<?php echo $i; ?>’><?php echo $i; ?>  &nbsp</a>
</li>
 <?php } ?>
<li>&nbsp &nbsp &nbsp &nbsp &nbsp</li> <?php
 echo ‘</ul>’;
echo ‘<br>’;
  }
  }
 } catch (PDOException $e) {
 echo ‘pb de connexion’;
 }
?>

Problème d’affichage des accents des données de la base de données (solution trouvée dans le forum Planethoster) :
Sélectionner l’interclassement utf8mb4_unicode_ci et rajouter en dernier de votre instance PDO le paramètre “array(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES utf8” :

$pdo = new PDO(‘mysql:host=localhost; dbname=bdd_name’, ‘username’, ‘pswd’, array(PDO::MYSQL_ATTR_INIT_COMMAND => “SET NAMES utf8”));

  

Problème d’affichage des accents en PHP :

1) Pour retirer des caractères spéciaux incluant le signe ‘=’, utiliser la fonction quoted_printable_decode :
$texte_converti1 = quoted_printable_decode($texte_a_convertir);       
          
2) Pour retirer des caractères spéciaux incluant le signe ‘?’, utiliser la fonction iconv :
$texte_converti2 = iconv(“iso-8859-1”, “UTF-8”, $texte_a_convertir);
NB : pour connaître le charset du texte d’un email, utliser la fonction imap_fetchheader($connection_imap, $email_number)

Les deux peuvent se cumuler.