How to take the first and the last day of a month in PHP

  • July 21, 2014
  • Boursinos Website
  • php
  • Comments Off on How to take the first and the last day of a month in PHP

While i was searching how to take the first and the last day of a month in php i found two functions that returns to you those dates.
Here are the two functions.
function lastday($month = ”, $year = ”) {
if (empty($month)) {
$month = date(‘m’);
}
if (empty($year)) {
$year = date(‘Y’);
}
$result = strtotime(“{$year}-{$month}-01″);
$result = strtotime(‘-1 second’, strtotime(‘+1 month’, $result));
return date(‘Y-m-d’, $result);
}

function firstDay($month = ”, $year = ”) {
if (empty($month)) {
$month = date(‘m’);
}
if (empty($year)) {
$year = date(‘Y’);
}
$result = strtotime(“{$year}-{$month}-01”);
return date(‘Y-m-d’, $result);
}

Cheers guys