Categories: Development

PHP function to get difference between two dates

If you want to get the minutes, hours, days, months between two dates in PHP this function is for you. Feel free to customize its return output to suit your needs as per the commented out time values.

function date_to_ago($old_date, $new_date){
$date1 = new DateTime($old_date);
$date2 = $date1->diff(new DateTime($new_date));
//$date2->days.' days
'; //$date2->y.' years
'; //$date2->m.' months
'; //$date2->d.' days
'; //$date2->h.' hours
'; //$date2->i.' minutes
'; //$date2->s.' seconds
'; return "$date2->i Minutes"; } echo date_to_ago('2018-02-4 9:28:07','2018-02-4 10:29:34');//61

In the example i got the minutes between two date times with the difference being 61 minutes.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago