'Laravel Carbon throws error The format separator does not match
I'm, trying to format a date into (Dayname Date Month) and I'm using Carbon for this but it return an error Carbon The format separator does not match here is a sample of DB data 2017-02-09 18:30:00.
Below is my code
Carbon::createFromFormat('l j F', $matchArr['matchTime'])->toDateTimeString()
Solution 1:[1]
to convert DB date "2017-02-09 18:30:00" to (Dayname Date Month) just use the php date function:
date("D d M", strtotime("2017-02-09 18:30:00"));
//outputs "Thu 09 Feb"
date("D d M", strtotime($matchArr['matchTime']);
Solution 2:[2]
So I just end up using native date function
date('l j F', strtotime($matchArr['matchTime']))
Solution 3:[3]
Assuming the value you're passing as your 2nd argument is a string 2017-02-09 18:30:00
, Carbon will throw this error because the format you've specified as the 1st argument - l j F
- doesn't match the format of the value you're passing as your 2nd argument.
2017-02-09 18:30:00
has format Y-m-d H:i:s
- i.e. the following should work:
Carbon::createFromFormat('Y-m-d H:i:s', '2017-02-09 18:30:00');
N.B. 1: In Carbon, the error
The format separator does not match
often comes up if you mix up your order of arguments - i.e. if you put the time, followed by the format. (Thought this was worth worth mentioning for anyone else who comes across this - an easy mistake to make!)
N.B. 2: Carbon uses the same date time formatting as native PHP DateTime: https://www.php.net/manual/en/datetime.format.php
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | hazelcodes |
Solution 2 | |
Solution 3 | Zilbert97 |