strtotime
es bastante potente con relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(esto sólo funciona 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');
Con el fin de conseguir el primero y el último día de un mes en PHP < 5.3, puede usar una combinación de mktime
y date
(date('t')
indica el número de días del mes):
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 lo que desea es obtener una cadena para su presentación, a continuación, no es necesario 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
Ver también: http://stackoverflow.com/questions/1897727/get-first -day-of-week-in-php –
* (referencia) * [Formatos de fecha relativos en PHP] (http://de2.php.net/manual/en/datetime.formats.relative.php) – Gordon