Category: PHP

To calculate the difference between two dates in years, months and daysin PHP, we have to use three functions Read Here, strtotime, floor and abs functions in PHP Let us consider two dates and store in variables.$date1 = ‘2016–06–01’;$date2 = ‘2020–08–08’; Now, the difference between these two dates will be $diff = abs(strtotime($date2)-strtotime($date1)); To calculate the year, divide the difference by total seconds in the year that is 365*60*60*24

Now, the month between these two dates will be $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); To calculate the day, subtract the date difference from the year and month and divide the result by total seconds in a day that is 60*60*24 Now, the days between these two dates will be $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); Complete Code:-
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners