php date function ( diffrence between two dates )

Dear Experts,

I'd like to find the time duration in days and months between two dates.

I have two dates

echo $today = date('Y-m-d');
echo "<br>";
echo $seconddate =  "2017-10-18";
echo "<br>";
echo $diffrence = ($seconddate - $today);

Open in new window


but it doesn't work. what should I do?
LVL 1
BRDigital MarketingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThomasCommented:
http://www.php.net/manual/en/datetime.diff.php

If you cast both dates to a DateTime, you can use the Diff method to calculate the difference between 2 dates.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SniperCode ShevaCommented:
Hello, try this:

<?php
$date1 = "now";
$date2 = "2017-10-18";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months, %d days\n", $years, $months, $days);
?>

Open in new window

BRDigital MarketingAuthor Commented:
thank you SniperCode Sheva
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

SniperCode ShevaCommented:
You are welcome.
Ray PaseurCommented:
The correct answer here is the first comment from Thomas.  It's an anti-practice to write your own date/time arithmetic, especially because it will often be wrong in months that do not have 30 days, in leap years, or across daylight savings time.  Instead use the built-in PHP functionality.

You can learn more about the ways we handle date/time values in this article.
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

You can get better answers here at E-E if you search the articles first, and if you leave your questions open a bit longer.  I would recommend leaving your questions open for at least 24 hours.

I do not recommend using the accepted solution in a deployed application.
BRDigital MarketingAuthor Commented:
Thank you Ray Paseur
You are right
Thank you very much

I will use the first answer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.