Home > database >  Date time format in php - Fatal error: Uncaught Error: Call to a member function format() on string
Date time format in php - Fatal error: Uncaught Error: Call to a member function format() on string

Time:09-27

I have this kind of error, and wondering if there's anyway I can fix this

  public function someFunction(int $referral_id)
    {
        $data = DB::table('someTables')
            ->where('referral_id', $referral_id)
            ->first();

        return $data ? (object) [
            'planId' => $data->plan_id ?? '',
            'comment' => $data->comment ?? '',
            'facilitator' => $data->facilitator ?? '',
            'time' => $data->started_at->format('H:i:s') ?? '',
        ] : (object) [];
    }

Fatal error: Uncaught Error: Call to a member function format() on string

that is the error message returning,

My database is saving date and time on a single column but I like them to be separated, is it possible?

Thanks Lads

CodePudding user response:

it looks like you're using it without a Model and so the data will be returned in it's raw format, in this case a string.

Using PHP date and strtotime should give you the time from the ISO8601 string

echo date("H:i:s", strtotime($data->started_at));
  • Related