strtotime
est assez puissant avec relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(fonctionne uniquement avec PHP 5.3+)
strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
Afin d'obtenir la première et la dernière date d'un mois en PHP < 5.3, vous pouvez utiliser une combinaison de mktime
et date
(date('t')
donne le nombre de jours du mois):
mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
Si vous voulez juste pour obtenir une chaîne pour la présentation, alors vous n'avez pas besoin mktime
:
date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Voir aussi: http://stackoverflow.com/questions/1897727/get-first -day-of-week-in-php –
* (référence) * [Formats de date relatifs en PHP] (http://de2.php.net/manual/fr/datetime.formats.relative.php) – Gordon