Home » Php » In PHP, is there an easy way to get the first and last date of a month?

In PHP, is there an easy way to get the first and last date of a month?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I need to get the first and last day of a month in the format YYYY-MM-DD given only the month and year. Is there a good, easy way to do this?

How to&Answers:
$first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
$last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));

See date() in PHP documentation.

Answer:

First day is always YYYY-MM-01, isn’t it? Example: date("Y-M-d", mktime(0, 0, 0, 8, 1, 2008))

Last day is the previous day of the next month’s first day:

$date = new DateTime("2008-09-01");
$date->modify("-1 day");
echo $date->format("Y-m-d");

Answer:

The first day of the month is always 1.
So it will become

YYYY-MM-01

the last day can be calculated as:

<?php
    $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
    echo "There was $num days in August 2003";
?>

Answer:

OK, first is dead easy.

date ('Y-m-d', mktime(0,0,0,MM,01,YYYY));

Last is a little trickier, but not much.

date ('Y-m-d', mktime(0,0,0,MM + 1,-1,YYYY));

If I remember my PHP date stuff correctly…

**edit – Gah! Beaten to it about a million times…

Edit by Pat:

Last day should have been

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1

Answer:

<?php
echo "Month Start - " . $monthStart = date("Y-m-1") . "<br/>";
$num = cal_days_in_month(CAL_GREGORIAN, date("m"), date("Y"));
echo "Monthe End - " . $monthEnd = date("Y-m-".$num);
?>

Answer:

The easiest way to do this with PHP is

$dateBegin = strtotime("first day of last month");  
$dateEnd = strtotime("last day of last month");

echo date("MYDATEFORMAT", $dateBegin);  
echo "<br>";        
echo date("MYDATEFORMAT", $dateEnd);

Or the last week

if (date('N', time()) == 7) {
$dateBegin = strtotime("-2 weeks Monday");
$dateEnd = strtotime("last Sunday");
} else {
$dateBegin = strtotime("Monday last week"); 
$dateEnd = strtotime("Sunday last week");   
}

Or the last year

$dateBegin = strtotime("1/1 last year");
$dateEnd = strtotime("12/31 this year");

Answer:

By the way @ZombieSheep solution

date ('Y-m-d', mktime(0,0,0,$MM + 1,-1,$YYYY));

does not work it should be

date ('Y-m-d', mktime(0,0,0,$MM + 1,0,$YYYY)); // Day zero instead of -1

Of course @Michał Słaby’s accepted solution is the simplest.

Answer:

Just to verify that I didn’t miss any loose ends:

$startDay = 1;

if (date("m") == 1) {
    $startMonth = 12;
    $startYear = date("Y") - 1;

    $endMonth = 12;
    $endYear = date("Y") - 1;
}
else {
    $startMonth = date("m") - 1;
    $startYear = date("Y");

    $endMonth = date("m") - 1;
    $endYear = date("Y");
}

$endDay = date("d") - 1;

$startDate = date('Y-m-d', mktime(0, 0, 0, $startMonth , $startDay, $startYear));
$endDate = date('Y-m-d', mktime(0, 0, 0, $endMonth, $endDay, $endYear));

Answer:

try this to get the number of days in the month:

$numdays = date('t', mktime(0, 0, 0, $m, 1, $Y));

Answer:

Example; I want to get first day and last day of current month.

$month   = (int) date('F');
$year    = (int) date('Y');

date('Y-m-d', mktime(0, 0, 0, $month + 1, 1, $year)); //first
date('Y-m-d', mktime(0, 0, 0, $month + 2, 0, $year)); //last

When you run this for instance at date 2015-01-09, the first and last values will be sequentially;

2015-01-01
2015-01-31

Tested.

Answer:

From here(get next month last day) that is marked as duplicated, so i can’t add comment there, but people can got bad answers from there.

Correct one for last day of next month:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-t'));

Correct one for first day of next month:

echo ((new DateTime(date('Y-m').'-01'))->modify('+1 month')->format('Y-m-01'));

Code like this will be providing March from January, so that’s not what could be expected.

echo ((new DateTime())->modify('+1 month')->format('Y-m-t'));