Home / Tips / Programming languages / Scripting languages / PHP / Calculating the difference between two dates using PHP

Calculating the difference between two dates using PHP

PHP
 
PHP
  • Currently 0 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 0/5 (0 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


To find out the difference between two dates use the class DateTime

$startDate = new DateTime( '2007-03-24 15:40:10' );
$endDate = new DateTime( '2009-07-12 17:56:20' );
$interval = $startDate->diff( $endDate );
echo 'Difference: ' . $interval->y . ' years, ' . $interval->m . ' months, ' . $interval->d . ' days, ' . $interval->h . ' hours, ' . $interval->i . ' minutes, ' . $interval->s . ' seconds';

The result:

Difference: 2 years, 3 months, 18 days, 2 hours, 16 minutes, 10 seconds

 

Read also

What is PHP?

What is PHP?

Classes and objects in PHP

Classes and objects in PHP

How to access classes and objects in PHP

How to access classes and objects in PHP

Post comment

basicuse.net
html5_css
106160485398655174790

Quick navigation

General navigation