Examine this self-explanatory code in PHP:
Reality:
$dateTime = Carbon::createFromDateTime(2017, 2, 23);
echo $dateTime; // 2017-02-23 00:00:00
echo $dateTime->startOfYear(); // 2017-12-31 23:59:59
echo $dateTime; // 2017-12-31 23:59:59
Notice that on the 4th line, the value of $dateTime
is 2017-12-31 23:59:59
. That is because on the 3rd line.
But why? I know that Carbon's startOfYear() is a modifier, but how can we therefore get a date's start of the year without modifying itself
Expected:
$dateTime = Carbon::createFromDateTime(2017, 2, 23);
echo $dateTime; // 2017-02-23 00:00:00
echo $dateTime->startOfYear(); // 2017-12-31 23:59:59
echo $dateTime; // 2017-02-23 00:00:00
Above, notice the 4th line. In reality, the 4th line outputs 2017-12-31 23:59:59
.
$modifiedDate = $dateTime; $modifiedDate->startOfYear();
– Sumbawa