1

How to make date string to date object in PHP using Carbon?

$minTime = DB::table('order') 
            ->select('order_creation_date', 'restaurant_id')
            ->where('restaurant_id', '=', '70')
            ->orderBy('order_creation_date', 'asc')
            ->first();

            $m = $orderDay -> month;
            $d = $orderDay -> day;
            $y = $orderDay -> year;

    $y = (int)$y;
    $d = (int)$d;
    $m = (int)$m;

    $orderDay = $minTime -> order_creation_date;
    var_dump($orderDay);

Error:

Trying to get property of non-object

Normal Value of string:

string(19) "2016-02-05 14:51:30"

Any ideas how to fix it? I think I need make string to date object. Thank you.

2 Answers 2

1

Using $dates property in Order model:

protected $dates = ['order_creation_date'];
Sign up to request clarification or add additional context in comments.

2 Comments

It returns a Carbon object when dealing with order_creation_date. Read about Carbon. @ArnasPuidokas
It doesn't suck. I just said in colloquial Persian, purposely. @WiktorStribiżew
0

Your error surely is on line 2

Change the order of the code:

$minTime = DB::table('order') 
        ->select('order_creation_date', 'restaurant_id')
        ->where('restaurant_id', '=', '70')
        ->orderBy('order_creation_date', 'asc')
        ->first();

$orderDay = $minTime -> order_creation_date;

        $m = $orderDay -> month;
        $d = $orderDay -> day;
        $y = $orderDay -> year;

$y = (int)$y;
$d = (int)$d;
$m = (int)$m;


var_dump($orderDay);

or when accessing the dates using DB facade, it probably doesn't return a Carbon Instance. Try ..:

$orderDay = Carbon::createFromFormat("Y-m-d H:i:s",$minTime -> order_creation_date);

Alternatively, you should prefer to use $dates property in your Order Model..

protected $dates = ['order_creation_date'];

5 Comments

@arnas-puidokas Updated.. Check-Again
Trailing data in Error
@arnas-puidokas Please explain the error. More description and line no. etc
in Carbon.php line 425 at Carbon::createFromFormat('Y-n-j G:i:s', '2016-02-05 14:51:30-8-1 11:53:58', null) in Carbon.php line 368 at Carbon::create('2016-02-05 14:51:30') in DashboardController.php line 77 at DashboardController->getDashboardView()
@arnas-puidokas Oh.. use the following instruction ... $orderDay = Carbon::createFromFormat("Y-m-d H:i:s",$minTime -> order_creation_date);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.