spires Posted June 8, 2009 Share Posted June 8, 2009 Hi Guys I have a dat stored on my database ($date = date('d/m/Y'). What I now need to do is see how much time has passed since this date. e.g 3 day have passed. Can anyone point me in the right direction so I can find out how to do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/161358-date-help/ Share on other sites More sharing options...
gaza165 Posted June 8, 2009 Share Posted June 8, 2009 <?php function smoothdate ($year, $month, $day) { return sprintf ('%04d', $year) . sprintf ('%02d', $month) . sprintf ('%02d', $day); } function date_difference ($first, $second) { $month_lengths = array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $retval = FALSE; if ( checkdate($first['month'], $first['day'], $first['year']) && checkdate($second['month'], $second['day'], $second['year']) ) { $start = smoothdate ($first['year'], $first['month'], $first['day']); $target = smoothdate ($second['year'], $second['month'], $second['day']); if ($start <= $target) { $add_year = 0; while (smoothdate ($first['year']+ 1, $first['month'], $first['day']) <= $target) { $add_year++; $first['year']++; } $add_month = 0; while (smoothdate ($first['year'], $first['month'] + 1, $first['day']) <= $target) { $add_month++; $first['month']++; if ($first['month'] > 12) { $first['year']++; $first['month'] = 1; } } $add_day = 0; while (smoothdate ($first['year'], $first['month'], $first['day'] + 1) <= $target) { if (($first['year'] % 100 == 0) && ($first['year'] % 400 == 0)) { $month_lengths[1] = 29; } else { if ($first['year'] % 4 == 0) { $month_lengths[1] = 29; } } $add_day++; $first['day']++; if ($first['day'] > $month_lengths[$first['month'] - 1]) { $first['month']++; $first['day'] = 1; if ($first['month'] > 12) { $first['month'] = 1; } } } $retval = array ('days' => $add_day); } } return $retval; } $begin = array ('year' => 2008, 'month' => 1, 'day' => 19); //START DATE HERE $end = array ('year' => date("Y"), 'month' => date("m"), 'day' => date("d")); //END DATE HERE $foo = date_difference ($begin, $end); foreach ($foo as $key => $value) { echo "<p>".$value." ".$key." have passed</p>"; } ?> here is a function i found on the web so i cant take credit for it...hope it helps Quote Link to comment https://forums.phpfreaks.com/topic/161358-date-help/#findComment-851496 Share on other sites More sharing options...
Adam Posted June 8, 2009 Share Posted June 8, 2009 $days_since = floor((time() - strtotime("04/04/2009"))/(60*60*24)); Just replace "04/04/2009" with the date you pull from the database. Quote Link to comment https://forums.phpfreaks.com/topic/161358-date-help/#findComment-851507 Share on other sites More sharing options...
spires Posted June 8, 2009 Author Share Posted June 8, 2009 Thanks guys. I shall try them out Quote Link to comment https://forums.phpfreaks.com/topic/161358-date-help/#findComment-851510 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.