Hi Everyone,
i have interesting problem, when i 02/2017 over GET sended, Carbon create wrong date? is createFromFormat not correct or this way not normal?
_Simple Request : index.php?month=02/2017_
echo $selected_date = Carbon::createFromFormat('m/Y', $request->get('month'));
_return_:
2017-03-02 23:44:44
Seems to be affected on a few random months. Works fine for Jan/March though.
it's work, when if a 01/2017 or 03/2017 - 04/.. - 12/.. sendet, . Only 02/** is problem.
Got the same Issue:
for ($i = 1; $i < 13; $i++) {
$m = $i < 10 ? '0' . $i : $i;
echo $m . ': '
. Carbon\Carbon::createFromFormat('m', $m)->format('m') // format for easier readablitiy
. PHP_EOL;
}
/*
Output:
Expected: Actual
01: 01
02: 03
03: 03
04: 05
05: 05
06: 07
07: 07
08: 08
09: 10
10: 10
11: 12
12: 12
*/
It looks as though you tested this on the 30th/31st. By not supplying the day, is it possible DateTime is creating the date 31st February, which is not possible and then jumping to the next month?
This happens because DateTime will always assume it is the current day when an alternative is not supplied as attribute.
Executed on the 22/10/2017
Carbon\Carbon::createFromFormat('m/Y', 11/2017);
Output: 2017-11-22
Executed on the 31/10/2017
Carbon\Carbon::createFromFormat('m/Y', 11/2017);
Output: 2017-12-01
As there is no 31st day in November the date skips to the first day of the next month.
You can supply the day attribute to always get the expected year/month result.
Carbon\Carbon::createFromFormat('d/m/Y', "01/11/2017");
Output: 2017-11-01
We will not change this behavior. For safe dates add the day.
@Jeemusu: just use ! format:
Carbon\Carbon::createFromFormat('!m/Y', '11/2017');
See here: http://php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters what ! format is:
Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch. Without !, all fields will be set to the current date and time.
@Jeemusu: just use
!format:Carbon\Carbon::createFromFormat('!m/Y', '11/2017');See here: http://php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters what
!format is:Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch. Without !, all fields will be set to the current date and time.
Thank you.
@Jeemusu: just use
!format:Carbon\Carbon::createFromFormat('!m/Y', '11/2017');See here: http://php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters what
!format is:Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch. Without !, all fields will be set to the current date and time.
Thank you man !
Most helpful comment
@Jeemusu: just use
!format:See here: http://php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters what
!format is: