简体   繁体   中英

How to calculate hour two date difference in php?

I have a time column is like this: "19-01-2022 12:49:00" also I need to current time.

I want to calculate current time minues my time column value but my result must be hours.

For example 19-01-2022 12:49:00 -> its my time and if current time 19-01-2022 13:49:05 result is a 1

I want to write if condition (currentTime-timeColumn>24) or (currentTime-timeColumn>48) like this.

You have a laravel tag and therefore I suggest using Carbon.

Example:

$dateOne = Carbon\Carbon::parse("19-01-2022 12:49:00");
$dateTwo =  Carbon\Carbon::parse("19-01-2022 13:49:00");

$dateTwo->diffInHours($dateOne); //gives you 1

Notes: You do not need to parse into Carbon if the column name is already mutated through $dates array in the model.https://laravel.com/docs/5.5/eloquent-mutators#date-mutators

Carbon documentation for reference: https://carbon.nesbot.com/docs/

Hope this points you in the right direction.

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