strtotime
là khá mạnh mẽ với relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(điều này chỉ làm việc với 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');
Để có được ngày đầu tiên và cuối cùng của một tháng trong PHP < 5.3, bạn có thể sử dụng kết hợp mktime
và date
(date('t')
cho số ngày trong tháng):
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
Nếu bạn chỉ muốn có được một chuỗi để trình bày, sau đó bạn không cần phải 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
Xem thêm: http://stackoverflow.com/questions/1897727/get-first -day-of-week-in-php –
* (tham khảo) * [Định dạng ngày tương đối trong PHP] (http://de2.php.net/manual/en/datetime.formats.relative.php) – Gordon