3

Hello there I need help in formatting my date before it reaches client side. Basically after getting the values from the database I want to format my date before it goes to the client side of my program like what I am doing here.

public function filterDataCal(Request $request) {

      $filtered_table = Method::leftJoin('users', 'users.id', '=', 'methods.created_by')
      ->leftJoin('roles', 'roles.id', '=', 'users.role_id')
      ->leftJoin('types', 'types.id', '=', 'methods.type_id')
      ->where('type_id', '=', $request->filters['type_id'])
      ->get([ 'users.username', 'users.id AS users_id', 'methods.*', 'methods.id AS method_id', 'methods.name AS method_name', 'roles.id AS role_id', 'roles.name AS role_name',
      'types.id AS type_id_typetable', 'types.name AS type_name']);
      if($filtered_table) {
        // for ($i=0; $i < sizeof($filtered_table); $i++){
        //
        //   // foreach ($filtered_table as $value[i]) {
        //   //   $value[i]->created_at->format("m/d/Y h:i A");
        //   // }
        // }
        foreach($filtered_table as $data) {
          $data->created_at = $data->created_at->format('m/d/y h:i A');
        }
        return $filtered_table;
      } else {
        return "";
      }
    }

I was not sure what kind of syntax I need to do to change the format of the date I have gotten from the database so what I did was foreach them then $data->created_at = $data->created_at->format('m/d/y h:i A'); but alas it won't go through, could you help me with changing that property (created_at) to a different format? Maybe I have something wrong with my syntax or maybe I there's a different approach. Thank you.

3 Answers3

1

There is nothing wrong in the syntax. You can simply print the date, you are returning the collecction.

foreach($filtered_table as $data) {
    echo  $data->created_at->format('m/d/y h:i A');
 }

If you need to customize the format while returning a collection, you can set the $dateFormat property on your model. See Date Mutators in documentation.

Sanzeeb Aryal
  • 4,358
  • 3
  • 20
  • 43
  • Is there are way to do that while only fetching the dates from the database? Or is it only for storing the dates? BTW I wanted to edit the property itself $data->created_at then return the array of object $filtered_table. –  Apr 17 '17 at 12:01
  • `$filtered_table` will return it's properties according to which they are set in model and by default timestamps are formatted as `'Y-m-d H:i:s'` to customize you can set `$dateFormat` property on your model. – Sanzeeb Aryal Apr 17 '17 at 13:05
0

You need to return $data->created_at instead $filtered_table .

Let me tell you why, you formatted the date and stored it into the variable name says $data->created_at but you are returning $filtered_table which is actually the raw date (You never formatted.)

Hope it helps :)

MD. Atiqur Rahman
  • 2,063
  • 4
  • 17
  • 30
  • But I need the whole $filtered_table to be returned. I just need to format that particular property $date->created_at then still return the whole array of objects. –  Apr 17 '17 at 11:57
  • Well, you need to run the `foreach` loop in the blade template again, right? Why don't you format the date there? – MD. Atiqur Rahman Apr 17 '17 at 12:14
  • I tried to do your suggestion but I am returning to a jquery render, it seems I can't use php format nor pass variables to it from a jquery render. –  Apr 17 '17 at 14:28
0

I ended up using a library called moment.js just to format the date in js

Javascript format date / time

Community
  • 1
  • 1