I'm currently using DateTime
objects to perform some calculations involving dates. For this instance, I'm trying to get the last day of the month using a pre-existing date. I tried the following code and wound up with an incorrect value for $endDate
, that being 1970-1-1
.
$startDate = new DateTime(date("Y-m-1"));
$endDate = new DateTime(date("Y-m-d", strtotime("last day of month", $startDate->format("Y-m-d"))));
echo "The start date should be 2016-1-1: " . $startDate->format("Y-m-d") . "<br />";
echo "The end date should be 2016-1-31: " . $endDate->format("Y-m-d") . "<br />";
How can I get it working correctly so that $endDate
comes out to the desired result? I don't want it to be this month specifically; it should work for any date string I supply via $startDate
.