I have the code:
$month = DateTime::createFromFormat('m/Y', $date);
if ($month) {
$month = $month -> format('01/m/Y');
}
echo "From ".$startMonth." to ".$endMonth = date("t/m/Y", strtotime($month));
when $date
is a string like "05/2015".
This returns:
From 01/05/2015 to 31/01/2015
But for some reason the month is coming up as 01
when it should be 05
? Why is it doing this? It should be
From 01/05/2015 to 31/05/2015
Besides the unnecessary chopping and changing between DateTime objects and unix timestamps (when you could do the whole thing using DateTime objects.... you're passing a formatted date of '01/m/Y'
to strtotime()
... the /
indicates to the strtotime()
function that the date is in US date format (mm/dd/yy
): and you should use '01-m-Y'
(with a -
) for dd-mm-YYYY
if you want to tell strtotime()
that it's a European format date.
See the "localized notations" table on the PHP Docs page for an explanation of formats accepted by strtotime()
However, doing the whole thing using DateTime objects:
$date = '4/2015';
$month = DateTime::createFromFormat('m/Y', $date);
echo "From " . $month->format('01/m/Y') . " to ". $month->format('t/m/Y');