Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TimiAde's avatar

Carbon Date error InvalidFormatException

My code

 $event = new Event(array(
            'summary' => $this->summary, #topic
            'location' => $this->location, #location
            'description' => $this->description . ' ' . $url, #description
            'start' => array(
            'dateTime' => Carbon::createFromFormat('Y-m-d H:i',$this->start_time), #error comes from this line
            'timeZone' =>$this->timezone, #timezone
            ),
            'end' => array(
            'dateTime' => Carbon::createFromFormat('Y-m-d H:i', $this->end_time), #timezone
            'timeZone' => $this->timezone, #timezone
            ),
            'recurrence' => array(
            'RRULE:FREQ=DAILY;COUNT=1'
            ),
            'attendees' => array(
            array('email' => $this->event_creator_email), #event_creator email
            array('email' => $this->booker_email), #booker email
            ),
            'reminders' => array(
            'useDefault' => FALSE,
            'overrides' => array(
                array('method' => 'email', 'minutes' => 24 * 60),
                array('method' => 'popup', 'minutes' => 10),
            ),
            ),
        ));

The error

Carbon\ Exceptions\ InvalidFormatException
Not enough data available to satisfy format
0 likes
11 replies
LaryAI's avatar
Level 58

The error message suggests that the format of the date string passed to createFromFormat is incorrect. The format string 'Y-m-d H:i' expects a date string in the format 'year-month-day hour:minute'.

Make sure that the value of $this->start_time and $this->end_time are in the correct format. If the format is different, you need to adjust the format string accordingly.

For example, if the date string is in the format 'month/day/year hour:minute', you would use the format string 'm/d/Y H:i':

'dateTime' => Carbon::createFromFormat('m/d/Y H:i', $this->start_time),

If the date string is in a different format, you can refer to the PHP documentation for the date() function to find the correct format string.

Also, make sure that the value of $this->timezone is a valid timezone identifier. You can check the list of supported timezones in the PHP documentation.

'timeZone' => 'America/New_York',

If the issue persists, you can try to debug the value of $this->start_time and $this->end_time to see if they contain the expected values.

tisuchi's avatar

@TimiAde Try this:

Carbon\Carbon::createFromFormat("Y-m-d H:i", $this->start_time);
TimiAde's avatar

@tisuchi i am getting the same error for this

dd(Carbon::createFromFormat('Y-m-d H:i',$this->start_time));

the error

Carbon\ Exceptions\ InvalidFormatException
Not enough data available to satisfy format
tisuchi's avatar

@TimiAde I am wondering. It works fine on my side.

Carbon\Carbon::createFromFormat('Y-m-d H:i',"2023-5-13 07:00");

Make sure your input has the same date format.

TimiAde's avatar

@tisuchi

class GoogleController extends Controller
{
    //
    // private $event;
    private $summary;
    private $location;
    private $description;
    public $start_time;
    public $end_time;
    private $timezone;
    private $event_creator_email;
    private $booker_email;

    public function oauth(){


        //all my properties are null here 


        dd($this->location); //null
        $uuid = Str::uuid();
        $url = "http:://127.0.0.1:8000/schedules/".$uuid."/";
        $event = new Event(array(
            'summary' => $this->summary, #topic
            'location' => $this->location, #location
            'description' => $this->description . ' ' . $url, #description
            'start' => array(
            'dateTime' => Carbon::createFromFormat('Y-m-d H:i',$this->start_time), #datetime
            'timeZone' => $this->timezone, #timezone
            ),
            'end' => array(
            'dateTime' => Carbon::createFromFormat('Y-m-d H:i', $this->end_time), #added time like 30 mins
            'timeZone' => $this->timezone, #timezone
            ),
            'recurrence' => array(
            'RRULE:FREQ=DAILY;COUNT=1'
            ),
            'attendees' => array(
            array('email' => $this->event_creator_email), #event_creator email
            array('email' => $this->booker_email), #booker email
            ),
            'reminders' => array(
            'useDefault' => FALSE,
            'overrides' => array(
                array('method' => 'email', 'minutes' => 24 * 60),
                array('method' => 'popup', 'minutes' => 10),
            ),
            ),
        ));
        
    }
    public function client(AppEvent $event){
        $this->start_time = $event->booking->booking_date . ' ' . $event->booking->start_time;
        $this->end_time = $event->booking->booking_date . ' ' . $event->booking->end_time;
        // dd($this->start_time); #right
        $this->summary = $event->eventname;
        $this->description = $event->description;
        $this->location = $event->bookingdetail->location;
        $this->timezone = $event->bookingevent->timezone;
        $this->event_creator_email = $event->user->email;
        $this->booker_email = $event->bookingdetail->email;
        // dd($this->start_time, $this->end_time, $this->summary, $this->description, $this->location, $this->timezone, $this->event_creator_email, $this->booker_email);
        $client = new Google_Client();
        $client->setAuthConfig(public_path('files/client_secret_68036643521-8ujgf4t0pndql4usb2qneori0ftg1htn.apps.googleusercontent.com.json'));
        $client->addScope(Calendar::CALENDAR);
        $client->setRedirectUri('http://127.0.0.1:8000/oauth2callback.php');
        // offline access will give you both an access and refresh token so that
        // your app can refresh the access token without user interaction.
        $client->setAccessType('offline');
        // Using "consent" ensures that your application always receives a refresh token.
        // If you are not using offline access, you can omit this.
        $client->setPrompt('consent');
    
        $client->setIncludeGrantedScopes(true);
        $email = Auth::user()->email;
    
        $client->setLoginHint($email);
        // $client->setApprovalPrompt('consent');
    
        $auth_url = $client->createAuthUrl();
        // dd(filter_var($auth_url, FILTER_SANITIZE_URL));
        return redirect(filter_var($auth_url, FILTER_SANITIZE_URL));

    }

This is the issue in the client method it is okay but in the oauth method alll the properties are null. What can be the issue.

tisuchi's avatar

@TimiAde How do you get these values?

private $summary;
    private $location;
    private $description;
    public $start_time;
    public $end_time;
    private $timezone;
    private $event_creator_email;
    private $booker_email;
TimiAde's avatar

@tisuchi from the $event variable parsed to the Controller

Route::get('/oauth2callback.php', [GoogleController::class, 'oauth']);
Route::get('/client/{event}', [GoogleController::class, 'client'])->name('google.create');

the variable

public function client(AppEvent $event){
        $this->start_time = $event->booking->booking_date . ' ' . $event->booking->start_time;
        $this->end_time = $event->booking->booking_date . ' ' . $event->booking->end_time;
        // dd($this->start_time); #right
        $this->summary = $event->eventname;
        $this->description = $event->description;
        $this->location = $event->bookingdetail->location;
        $this->timezone = $event->bookingevent->timezone;
        $this->event_creator_email = $event->user->email;
        $this->booker_email = $event->bookingdetail->email;
....

First i enter this route

Route::get('/client/{event}', [GoogleController::class, 'client'])->name('google.create');

it then redirects to this route

Route::get('/oauth2callback.php', [GoogleController::class, 'oauth']);

Can i redirect with parameters

        return redirect(filter_var($auth_url, FILTER_SANITIZE_URL));

Can you explain to me how to redirect with parameters

umerfayyaz's avatar

try this

Carbon::parse($this->start_time)->toDateTimeString();

Snapey's avatar

2023-5-13 is not in 'Y-m-d' format because that would need to be 2023-05-13

A single digit month would need to be in this format 'Y-n-d' or possibly 'Y-n-j' (depending if the day is also potentially single digit)

Please or to participate in this conversation.