Type error: DateTime::__construct() expects parameter 1 to be string, object given in laravel

11,339

You're passing the whole object instead of its property, so change this:

DateTime($systemIntime)

To:

DateTime($systemIntime->intime)
Share:
11,339
Rashed Hasan
Author by

Rashed Hasan

C, PHP, Laravel, Java, Andriod, Javascript, VueJs, ReactJs, CSS, Bootstrap For Fun: discord

Updated on June 08, 2022

Comments

  • Rashed Hasan
    Rashed Hasan almost 2 years

    I am trying to get late time. $latetime1 returning correct but $latetime2 not returning same as $latetime1. $systemIntime value format and $inTime value format are the same. But I am getting above error for $latetime2. What should be the correct one, please someone help me. Here is my function bellow -

    public function update(Request $request, Attendance $attendance)
    {
        $attendance = Attendance::find($attendance->id);
        $inTime = $attendance->intime;
        // late time caculate
        $systemIntime = DB::table('schools')
                         ->join('users', 'schools.id', '=', 'users.school_id')
                         ->select('schools.intime')
                         ->first();
        $latetime1 = (new \DateTime($inTime))->format('H:i:s');
        $latetime2 = (new \DateTime($systemIntime))->format('H:i:s');
        $late      = $latetime1->diff($latetime2);