When using createFromFormat without a time, instead of using the time fixed by setTestNow, it use current time of the server which is not the expected behavior.
To reproduce:
<?php
require('vendor/autoload.php');
Carbon\Carbon::setTestNow(Carbon\Carbon::createFromFormat("Y-m-d H:i:s", '2018-01-01 00:00:00'));
$date = Carbon\Carbon::createFromFormat("Y-m-d", "2018-05-06");
$date->hour == 0 || die("Invalid time");
?>
which is not the expected behavior.
PHP manual states that this is the default behaviour...
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
Read more here: http://php.net/manual/en/datetime.createfromformat.php
On the link look at the format character !:
Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch
Based on that, try code bellow, which should reset time part to 00:00:00.
$date = Carbon\Carbon::createFromFormat("!Y-m-d", "2018-05-06");
The expected behavior is getting the time from the Carbon::setTestNow, not the one from the server. It messes up easily a lot of unit test if you test carbon instances against one another.
We found out this issue in my team because some tests were randomly failing even though we did fix the date with setTestNow.
Will be fixed in 1.28 thanks to @imbrish #1281
Most helpful comment
PHP manual states that this is the default behaviour...
Read more here: http://php.net/manual/en/datetime.createfromformat.php
On the link look at the format character
!:Based on that, try code bellow, which should reset time part to
00:00:00.