0

I want to convert my created_at date into day_of_week format, and I am stuck.

Here is what I did so far :

$max_created_at = DB::table('inventories')->max('created_at'); 
// 2015-02-17 21:32:30

$day = DateHelper::getDay($max_created_at); 
// 17

With the data I have, I want to convert my $day into day_of_week.

Example, since today is 2/18/2015 Wednesday, it should return 3.

  • Whats the `DateHelper` class? That isn't part of Laravel.. – Dan Smith Feb 18 '15 at 16:07
  • That is my own helper function that I wrote. With the function `getDay()` will spit out the day only (18) - for today. –  Feb 18 '15 at 16:08
  • Just ask the underlaying database for it (if feasible) or use PHP's DateTime class to parse the date and then ask the instance of DateTime for the day of week. see http://docs.php.net/datetime – VolkerK Feb 18 '15 at 16:10
  • possible duplicate of [PHP day of week](http://stackoverflow.com/questions/12835133/php-day-of-week) – Bang Feb 18 '15 at 16:11

2 Answers2

2

You don't need the second line to convert to the $day. Since the created_at column is an instance of Carbon, you can just do:

$max_created_at->format('N');

Where N is the "ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)" according to the manual.

Edit: My bad - you probably don't have an instance of Carbon in your case. But you can do date('N', strtotime($max_created_at)), that'll work equally fine.

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
0

This will do the trick :

    echo date('N', strtotime($max_created_at));
Jay
  • 103
  • 2
  • 10