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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago