简体   繁体   中英

Difference between 2 dates (not considering years)

I have a database with different domains and the dates when they were registered and I need to show when they will be expired (1 year registration). For example:

domain1.com - registered 11/19/2012 (mm-dd-yyyy) - expires in 365 days. domain2.com - registered 11/20/2011 (mm-dd-yyyy) - expires in 1 day.

I have been looking for different options and questions (ie How to calculate the difference between two dates using PHP? ) but they are a little bit different (I don't need years to take into account) and I am not an expert in PHP and dates.

I think that one option could be removing from the total the difference of years (*365) but I will not take into account years of 366.

Thank you in advanced! Regards,

Paul

You may useful this example.

$date1 = "11/19/2012";
$date2 = "11/20/2011";
$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);

Try it & provide appropriate response !!

Alternatively you could use the databases date functions to work this out for you.. with mysql it would be along the lines of

SELECT DATEDIFF(start_date, NOW()) AS days_remaining FROM table_name

See here for more info:

http://www.w3schools.com/sql/func_datediff_mysql.asp

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM