paschim Posted September 17, 2008 Share Posted September 17, 2008 In my database i am storing the date1 using time() of php! Now how do i get the number of years and months that has passed since date1? Link to comment https://forums.phpfreaks.com/topic/124610-solved-simple-noob-question-how-to-get-days-elapsed/ Share on other sites More sharing options...
genericnumber1 Posted September 17, 2008 Share Posted September 17, 2008 <?php $start = strtotime('5/21/1988'); // Start time, for instance, my birthday! $end = time(); // End time, today. $sDate = date('n/j/Y', $start); $eDate = date('n/j/Y', $end); list($eMonth, $eDay, $eYear) = explode('/', $eDate); list($sMonth, $sDay, $sYear) = explode('/', $sDate); $yearDiff = $eYear - $sYear; $monthDiff = $eMonth - $sMonth; $dayDiff = $eDay - $sDay; if($dayDiff < 0) { --$monthDiff; $dayDiff = date('t', $sMonth) - $sDay + $eDay; } if($monthDiff < 0) { --$yearDiff; $monthDiff += 12; } echo "$yearDiff years $monthDiff months $dayDiff days"; ?> It's accurate to the day, if you want hours/minutes/seconds... go for it! Also, this could be a little more efficient (less repeated code, etc) but I didn't do that to keep it simple so it's easy for you to read if you want to break it up into functions, go for it. EDIT: just a sec, messed something up let me fix it. Another EDIT: okay, fixed the date calculation, it's 3 am, give me a break Link to comment https://forums.phpfreaks.com/topic/124610-solved-simple-noob-question-how-to-get-days-elapsed/#findComment-643583 Share on other sites More sharing options...
genericnumber1 Posted September 17, 2008 Share Posted September 17, 2008 Okay, you wanted month/year, right? here you go. <?php $start = strtotime('2/28/1988'); // Start time, for instance, my birthday! $end = strtotime('6/27/2008'); // End time, today. $sDate = date('n/j/Y', $start); $eDate = date('n/j/Y', $end); list($eMonth, $eDay, $eYear) = explode('/', $eDate); list($sMonth, $sDay, $sYear) = explode('/', $sDate); $yearDiff = $eYear - $sYear; $monthDiff = $eMonth - $sMonth; $dayDiff = $eDay - $sDay; if($dayDiff < 0) { --$monthDiff; } if($monthDiff < 0) { --$yearDiff; $monthDiff += 12; } echo "$yearDiff years $monthDiff months"; doesn't work for day, but hey, you didn't say you wanted that working... tell me when it isn't 3:30 am if you want me to fix it, it's just annoying because of the difference in numbers of days/month I'd have to rewrite half of it to get it working correctly Link to comment https://forums.phpfreaks.com/topic/124610-solved-simple-noob-question-how-to-get-days-elapsed/#findComment-643585 Share on other sites More sharing options...
paschim Posted September 17, 2008 Author Share Posted September 17, 2008 Thank you Guys!! I used a different and simpler approach, i cooked up while experimenting! But your codes also taught me new things! Link to comment https://forums.phpfreaks.com/topic/124610-solved-simple-noob-question-how-to-get-days-elapsed/#findComment-643641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.