Jump to content

dirrefence between datetime values


robcrozier

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.