robcrozier Posted February 4, 2008 Share Posted February 4, 2008 Hi everyone, i wonder if anyone can tell me how i could find the difference between two datetime values that are taken from a MySQL database. I want the difference between the times to display something like: 1d 2h 23m. I've done a bit of research and all i can find is how to ind the difference between two times or two dates separately. I'm not even sure if a datetime difference can be found? Can anyone help? Thanks Link to comment https://forums.phpfreaks.com/topic/89350-dirrefence-between-datetime-values/ Share on other sites More sharing options...
haku Posted February 4, 2008 Share Posted February 4, 2008 date() time() mktime() Convert everything into unix timestamps. Link to comment https://forums.phpfreaks.com/topic/89350-dirrefence-between-datetime-values/#findComment-457564 Share on other sites More sharing options...
craygo Posted February 4, 2008 Share Posted February 4, 2008 made this function a while ago. There may be better one's online somewhere but.. <?php function datediff($date1, $date2){ if($date1 > $date2){ $month1 = date("m", $date1); $day1 = date("d", $date1); $year1 = date("Y", $date1); $hour1 = date("G", $date1); $min1 = date("i", $date1); $sec1 = date("s", $date1); $month2 = date("m", $date2); $day2 = date("d", $date2); $year2 = date("Y", $date2); $hour2 = date("G", $date2); $min2 = date("i", $date2); $sec2 = date("s", $date2); } else { $month1 = date("m", $date2); $day1 = date("d", $date2); $year1 = date("Y", $date2); $hour1 = date("G", $date2); $min1 = date("i", $date2); $sec1 = date("s", $date2); $month2 = date("m", $date1); $day2 = date("d", $date1); $year2 = date("Y", $date1); $hour2 = date("G", $date1); $min2 = date("i", $date1); $sec2 = date("s", $date1); } //seconds if($sec1 < $sec2){ $secdiff = ($sec1+60)-$sec2; $min1--;; } else { $secdiff = $sec1-$sec2; } // minutes if($min1 < $min2){ $mindiff = ($min1+60) - $min2; $hour1--; } else { $mindiff = $min1 - $min2; } // hours if($hour1 < $hour2){ $hourdiff = ($hour1+24) - $hour2; $day1--; } else { $hourdiff = $hour1 - $hour2; } $amountdays = date("t", $month1); // day if($day1 < $day2){ $daydiff = ($day1+$amountdays) - $day2; $month1--; } else { $daydiff = $day1 - $day2; } // months if($month1 < $month2){ $monthdiff = ($month1+12) - $month2; $year1--; } else { $monthdiff = $month1 - $month2; } // years $yeardiff = $year1 - $year2; $datediff = $yeardiff."Yrs ".$monthdiff."Mon ".$daydiff."Days ".$hourdiff."Hrs ".$mindiff."Min ".$secdiff."Sec "; return $datediff; } /* Uncomment below to use your corrent form */ $date1 = mktime('16', '20', '15', '12', '14', '2008'); $date2 = mktime('18', '30', '30', '1', '2', '2008'); echo datediff($date1, $date2); ?> The dates should be in unixtime. Ray Link to comment https://forums.phpfreaks.com/topic/89350-dirrefence-between-datetime-values/#findComment-457690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.