strtotime
è abbastanza potente, con relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(questo funziona solo con 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');
Al fine di ottenere la prima e l'ultima data di un mese in PHP < 5.3, è possibile utilizzare una combinazione di mktime
e date
(date('t')
indica il numero di giorni del mese):
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
Se si desidera solo per ottenere una stringa per la presentazione, quindi non è necessario 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
Vedi anche: http://stackoverflow.com/questions/1897727/get-first -day-of-week-in-php –
* (riferimento) * [Formati data relativi in PHP] (http://de2.php.net/manual/en/datetime.formats.relative.php) – Gordon