Yves Gonzaga Yves Gonzaga - 3 years ago 129
MySQL Question

How to get first and last date of the month from a given numeric month PHP

I want to get the first date and last date of the month if given I have a numeric number of the month.

For example January = 01, October = 10, November = 11

So if I pass 10 as parameter I should get the first and last date of October.

I tried to implement it using Carbon. But I gives me different result.

Here is the code. The result is is next to it.

$dt = Carbon::create(10);
echo $dt->startOfMonth(); // 0010-10-01 00:00:00
echo $dt->endOfMonth(); // 0010-10-31 00:00:00

Answer Source

The first day of any month is the first.

use createFromFormat

$dt = Carbon::createFromFormat('m', 10);
echo $dt->endOfMonth();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download