1

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 (i.e. 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

Community
  • 1
  • 1
TerminatorX
  • 307
  • 3
  • 13
  • So basically, you want to find the difference between "*the date plus one year* and *today*", expressed in days, right? There's a convenient method for PHP 5.3+ and a not so convenient method for PHP 5.2-. Which do you need? – deceze Nov 19 '12 at 11:18
  • http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php This might help you – Akhilraj N S Nov 19 '12 at 11:18
  • Hi deceze, I should have clarified it since the begging. I need it for PHP 5.3. thx. – TerminatorX Nov 19 '12 at 12:05
  • Hi Akhilraj N S. I have already checked that answer but unfortunately it doesn't work for me. I have tried to modify the code in order to get the result I'm expecting but I think I did something wrong somewhere :\ but thx for your reply! – TerminatorX Nov 19 '12 at 12:07

2 Answers2

0

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 !!

Daxen
  • 508
  • 4
  • 18
  • Hi Dev, thx! for your response. This is the same response as question http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php I took a look at it but I didn't work for me. Anyway I couldn't find anything accurate so I am implementing your solution adding 365 * ($years) + 31 * ($months) and taking into account leap years. I just need to figure out how to add the corresponding amount of days to each month but I think I will create some array for that. – TerminatorX Nov 20 '12 at 15:39
0

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

Dale
  • 10,384
  • 21
  • 34
  • That seems to be a really good solution! I tried to implement it in mysql and it returns some negative values that are not OK. I tried to add SELECT DATEDIFF(DAY(start_date), DAY(NOW())) and so on but it returns null. I have also tried but still not able to implement it from PHP. – TerminatorX Nov 20 '12 at 15:34