Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SankalpRanade's avatar

Date Conversion Issue.

I am inserting date as timestamp datatype while inserting a record, i need to count difference between current date and the 'updated_at' date field of database in number of days.

I have explored Carbon api, but i am getting issue in date format matching as database has timestamp format and carbon has different.

How to get difference of date in days...??

I have fetched the date in same format but i don't know how to count difference in days.

$get_plan_date=vendor_plan::select('updated_at')->where('vendor_id',$id)->first(); // returns "updated_at": "2017-07-03 12:10:29"

$current_date = Carbon::now(); // returns 2017-07-03 12:23:56

please help me out..!!

0 likes
2 replies
mushood's avatar
mushood
Best Answer
Level 41

You can convert your timestamp to carbon like this

$updated_at = Carbon::parse($get_plan_date);
$current_date = Carbon::now(); 
$difference = $updated_at -> diffInDays($current_date, false);

You could also do : $difference = $updated_at -> diffInDays(null, false); because of this rule: "Each function below has a default first parameter which is the Carbon instance to compare to, or null if you want to use now()."

Check here is you want to have difference in hours and so on http://carbon.nesbot.com/docs/#api-addsub

1 like

Please or to participate in this conversation.