Assume system time is set to 2017-03-30. Then this code will convert the date wrong:

<?php $dateTime = DateTime::createFromFormat('m-Y', '02-2017'); $converted = $dateTime->format('Y-m'); print_r($converted);

The value of $converted is

2017-03

but only when run from the browser. Run from command line, it gives the correct result 2017-02 .

Anyone knows why? February does not have 30 days, so that might be a reason, but still.

Edit: Changed format from 'Y-m-d' to 'Y-m'.

Edit 2: Added information about command line vs browser.