简体   繁体   中英

how to get date difference

In laravel i am using this :

@php
 $ids = Session::get('id');
 $res = DB::table('user')
       ->select('dates as date')
       ->where('id','=',$user_id)
       ->get();
$date_time=$res[0]->date;
$date = new DateTime($date_time);
$now = date('Y-m-d');
$dayss_left=$date->diff($now)->format("%d");
@endphp

//In $date_time i am getting :"2018-06-28"
//And In $now getting:        "2018-02-25"  

Its returning me erroe like:

DateTime::diff() expects parameter 1 to be DateTimeInterface, string given

What i am doing wrong here can anyone please tell i want days only

You are passing a string from date . You could create a DateTime :

new DateTime($now)

You could update your code to:

$dayss_left=$date->diff(new DateTime($now))->format("%d");

To create a DateTime in a specific format, you use createfromformat :

$date = DateTime::createFromFormat('Y-m-d', $date_time);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM