La gestion des dates en PHP

getdate() pour obtenir des informations sur une date

[pastacode lang= »markup » manual= »%24date%20%3D%20getdate()%3B%0Aprint_r(%24date)%20%3B%0A%0A%2F*%20%0ARetournera%20pour%20le%2015%20juin%202018%20%C3%A0%2012h30%20%3A%0AArray%0A(%0A%20%20%20%20%5Bseconds%5D%20%3D%3E%2056%0A%20%20%20%20%5Bminutes%5D%20%3D%3E%2030%0A%20%20%20%20%5Bhours%5D%20%3D%3E%2012%0A%20%20%20%20%5Bmday%5D%20%3D%3E%2015%0A%20%20%20%20%5Bwday%5D%20%3D%3E%205%0A%20%20%20%20%5Bmon%5D%20%3D%3E%206%0A%20%20%20%20%5Byear%5D%20%3D%3E%202018%0A%20%20%20%20%5Byday%5D%20%3D%3E%20165%0A%20%20%20%20%5Bweekday%5D%20%3D%3E%20Friday%0A%20%20%20%20%5Bmonth%5D%20%3D%3E%20June%0A%20%20%20%20%5B0%5D%20%3D%3E%201529058656%0A)%0A%0A%0A*%2F » message= » » highlight= » » provider= »manual »/]

strtotime() : Pour transformer un texte anglais en timestamp

int strtotime ( string $time [, int $now = time() ] )

$time : Une chaîne date/heure. (http://php.net/manual/fr/datetime.formats.php)
$now : Le timestamp, représentant la date courante, utilisé pour le calcul relatif des dates.

[pastacode lang= »php » manual= »%2F%2F%20Toutes%20les%20lignes%20suivantes%20retournent%20le%20timestamp%20de%20la%20date%20%3A%0Aecho%20strtotime(%22now%22)%3B%20%0Aecho%20strtotime(%2210%20September%202000%22)%3B%20%0Aecho%20strtotime(%22%2B1%20day%22)%3B%20%0Aecho%20strtotime(%22%2B1%20week%22)%3B%20%0Aecho%20strtotime(%22%2B1%20week%202%20days%204%20hours%202%20seconds%22)%3B%20%0Aecho%20strtotime(%22next%20Thursday%22)%3B%20%0Aecho%20strtotime(%22last%20Monday%22)%3B%20%0Aecho%20strtotime(%22this%20week%22)%3B » message= » » highlight= » » provider= »manual »/]

date() : Pour formater une date et heure locale

string date ( string $format [, int $timestamp = time() ] )

$format : http://php.net/manual/fr/function.date.php

[pastacode lang= »php » manual= »%2F*%20Retourne%202017-09-28%20*%2F%0Aecho%20date(‘Y-m-d’)%3B%20%0A%0A%2F*%20Retourne%20Thu%2C%2028%20Sep%202017%20*%2F%0Aecho%20date(‘D%2C%20d%20M%20Y’)%3B%20%2F%2F%20%0A%0A%2F*%20Retourne%20Sun%2C%2028%20Sep%202017%20*%2F%0Aecho%20date(‘D%2C%20d%20M%20Y’%2C%20strtotime(%22now%22))%3B » message= » » highlight= » » provider= »manual »/]

La classe DateTime : Représentation d’une date et heure

public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

$time : Une chaîne date/heure (http://php.net/manual/fr/datetime.formats.php)
Retourne une nouvelle instance de DateTime

[pastacode lang= »php » manual= »%2F*%20Retourne%20Thu%2C%2028%20Sep%202017%20%20*%2F%0A%24date%20%3D%20new%20DateTime()%3B%20%0Aecho%20%24date-%3Eformat(‘D%2C%20d%20M%20Y’)%3B%20%0A%0A%2F*%20%0A*%20Soustrait%20une%20dur%C3%A9e%20%C3%A0%20un%20objet%20DateTime%20en%20jour%20%0A*%20Retourne%20Thu%2C%2021%20Sep%202017%20%0A*%2F%0A%24date%20%3D%20new%20DateTime(date(‘Y-m-d’))%3B%20%0A%24date-%3Esub(new%20DateInterval(‘P7D’))%3B%20%0Aecho%20%24date-%3Eformat(‘D%2C%20d%20M%20Y’)%3B%20%0A%0A%2F*%20%0A*%20Ajoute%20une%20dur%C3%A9e%20%C3%A0%20un%20objet%20DateTime%20en%20jour%20%0A*%20Retourne%20Thu%2C%2005%20Oct%202017%20%0A*%2F%0A%24date%20%3D%20new%20DateTime(date(‘Y-m-d’))%3B%20%0A%24date-%3Eadd(new%20DateInterval(‘P7D’))%3B%20%0Aecho%20%24date-%3Eformat(‘D%2C%20d%20M%20Y’)%3B%20%0A%0A%2F*%20%0A*%20Ajoute%20une%20dur%C3%A9e%20%C3%A0%20un%20objet%20DateTime%20en%20heure%2C%20minute%20ou%20seconde%20%0A*%20Retourne%20Thu%2C%2028%20Sep%202017%2000%3A30%0A*%2F%0A%24date%20%3D%20new%20DateTime(date(‘Y-m-d’))%3B%20%0A%24date-%3Eadd(new%20DateInterval(‘PT30M’))%3B%20%0Aecho%20%24date-%3Eformat(‘D%2C%20d%20M%20Y%20H%3Ai’)%3B%20″ message= » » highlight= » » provider= »manual »/]

strftime() : Pour formater une date et heure en français

string strftime ( string $format [, int $timestamp = time() ] )

$format : http://php.net/manual/fr/function.strftime.php
$timstamp : timestamp Unix de type entier qui vaut par défaut l’heure courante locale si le paramètre timestamp n’est pas fourni. En d’autres termes, il vaut par défaut la valeur de la fonction time().

[pastacode lang= »php » manual= »setlocale(LC_TIME%2C%20%22fr_FR.UTF-8%22)%3B%20%0A%0A%2F*%20Retourne%20Samedi%2C%2001%20mai%202010%20*%2F%0Aecho%20strftime(‘%25A%2C%20%25d%20%25B%20%25G’%2C%20strtotime(‘2010-05-01’))%3B%20%0A%0A%2F*%20Retourne%20Lundi%2C%2025%20septembre%202017%20%20*%2F%0Aecho%20strftime(‘%25A%2C%20%25d%20%25B%20%25G’%2C%20strtotime(‘this%20week’))%3B%20%0A%0A%2F*%20Retourne%20Jeudi%2C%2021%20septembre%202017%20%20*%2F%0A%24date%20%3D%20new%20DateTime()%3B%20%0A%24date-%3Esub(new%20DateInterval(‘P7D’))%3B%20%0Aecho%20strftime(‘%25A%2C%20%25d%20%25B%20%25G’%2C%20%24date-%3EgetTimestamp())%3B%20%0A » message= » » highlight= » » provider= »manual »/]

 


Si cet article "La gestion des dates en PHP" vous a aidé, si vous avez des questions ou des améliorations à proposer, n'hésitez pas à me laisser un commentaire en bas de page !
Charlie Stram,
Développeur web freelance

Vous aimerez aussi...

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *