PHP: Calendar

To get number of months:

// Now: 19 November 2022
echo date('t');

// Using PHP DateTime Class
$date = new DateTime();
echo $date->format('t');
// OR
echo date_format('t', $date);

// 19 October 2022
echo date('t', strtotime('last month'));

// Using PHP DateTime Class
$lastmonth = new DateTime('last month');

echo $date->format('t');
// OR
echo date_format($date, 't');

To get first day of the month:

// Now: 19 November 2022
echo date('N', strtotime('first day of'));

// Using PHP DateTime Class
$firstdayofthemonth = new DateTime('first day of');

echo $firstdayofthemonth->format('N');
// OR
echo date_format($date, 'N');

References:


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *