Porl123 Posted March 6, 2011 Share Posted March 6, 2011 I have a php script that allows the user to enter a day, month and year, for example 10/10/2001. What I'm trying to do is find out how many years that date is from now, taking into account the days and months along with the years. So say I have the variables: <?php $day = 10; $month = 10; $year = 2001; ?> I've been thinking about this for a while now and I can't really think of how I would do it. If anyone can give me a hand I'd really appreciate it. (: Thanks! Link to comment https://forums.phpfreaks.com/topic/229762-date-question/ Share on other sites More sharing options...
ZacTopher Posted March 6, 2011 Share Posted March 6, 2011 first you find the difference between the two years and then you should have two if statements checking the following 1. is the given month higher than the current month 2. is the given month equal to the current month, if so, is the given day higher than the current day? if any of those are true you should substract 1 from the year difference NOTE: If you don't check if the year is zero then you will end up with -1 Link to comment https://forums.phpfreaks.com/topic/229762-date-question/#findComment-1183516 Share on other sites More sharing options...
Porl123 Posted March 6, 2011 Author Share Posted March 6, 2011 Thanks for the reply. Does this take leap years into account, btw? Link to comment https://forums.phpfreaks.com/topic/229762-date-question/#findComment-1183523 Share on other sites More sharing options...
Porl123 Posted March 6, 2011 Author Share Posted March 6, 2011 Or rather the different lengths of each month? Link to comment https://forums.phpfreaks.com/topic/229762-date-question/#findComment-1183525 Share on other sites More sharing options...
ZacTopher Posted March 6, 2011 Share Posted March 6, 2011 that checks only the day, month and year against the current date.. Link to comment https://forums.phpfreaks.com/topic/229762-date-question/#findComment-1183544 Share on other sites More sharing options...
Porl123 Posted March 6, 2011 Author Share Posted March 6, 2011 Ah, I've finally got my head around it. Thanks for the help Zac Link to comment https://forums.phpfreaks.com/topic/229762-date-question/#findComment-1183554 Share on other sites More sharing options...
ZacTopher Posted March 6, 2011 Share Posted March 6, 2011 You're welcome Link to comment https://forums.phpfreaks.com/topic/229762-date-question/#findComment-1183705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.