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
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.
2

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

0
BRMarketingAuthor Commented:
thank you SniperCode Sheva
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

SniperCode ShevaCommented:
You are welcome.
0
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.
1
BRMarketingAuthor Commented:
Thank you Ray Paseur
You are right
Thank you very much

I will use the first answer
0
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.