PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] Laravel Create Date return different format

 January 01, 2022     eloquent, laravel, php     No comments   

Issue

I tried to select the create_data from the database, the date format in the database is like "2021-05-18 11:06:01", but when I retrieve and display it, it's will show the format like is "2021-05-18T03:06:01.000000Z". Does anyone know why will become like that? I want the date which is actually like the database store which is "2021-05-18 11:06:01". However, the create_date inside DB the format is UTC +8, but when receive it will show UTC format.

enter image description here

Data return

[0] => Array (
    [id] => 1
    [log_name] => login
    [description] => login
    [subject_type] => App\Models\Users
    [subject_id] => 0
    [causer_type] => App\Models\User
    [causer_id] => 2
    [properties] => ""         
    [created_at] => 2021-05-18T03:06:01.000000Z
);

Code

$lastLoggedActivity = ActivityLog::where('causer_id', $userid)
    ->orWhereIn('subject_id', $selectparentid)
    ->with('getLogType')
    ->with('getCauserDetails')
    ->orderBy('created_at', 'desc')
    ->get()
    ->toArray();

Solution

Add a serializeDate method to your model to change it's format on responses.

/**
 * Prepare a date for array / JSON serialization.
 *
 * @param  \DateTimeInterface  $date
 * @return string
 */
protected function serializeDate(DateTimeInterface $date)
{
    return $date->format('Y-m-d H:i:s');
}


Answered By - jrcamatog
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing