I Used <?php $febenddate=date('Y-2-t'); ?>
But Result is 2021-02-31, How can i get 2021-02-28 ?
I Used <?php $febenddate=date('Y-2-t'); ?>
But Result is 2021-02-31, How can i get 2021-02-28 ?
You can use DateTime with a relative date expression.
$dateTime = date_create('Last Day of February 2021');
To output the date in the desired format:
echo $dateTime->format('Y-m-d'); //2021-02-28
If the year is omitted, February of the current year is used.
$dateTime = date_create('Last Day of February');
You can use the function cal_days_in_month()
You need to provide the month and year for which you need to find the total no of days (or the end date)
cal_days_in_month (int $calendar, int $month, int $year) : int
In your case:
echo cal_days_in_month(CAL_GREGORIAN, 2, 2021);
To get the last day of February of the current year, first make a timestamp for any day in February—for which the 1st of February is a good choice—and get its t
value. Just writing 2
in your date
format string doesn't make it choose the t
value of February:
echo date('Y-m-t', mktime(0, 0, 0, 2, 1));
Try this one:
echo date("Y-m-t", strtotime("2021-02-14"));
The "t" in "Y-m-t" returns number of days in given month (param set in strtotime function). In short: you are echoing out: Y: Year, m: Month, t: Number of days in the given month in given year.
You can get the last date of Month using the below PHP Code ....
<?php
$d = new DateTime( '2021-02-01' );
echo $d->format( 'Y-m-t' );
?>