2

I have in my database 4 columns which are:

Date_in | Time_in | Date_out | Time_out

Date_in and Time_in belong together (e.g., 2013-02-18 13:00:00) and date_out goes with Time_out. I would like to find out the difference between and I have gotten up till:

$start_time = new DateTime("'$list[date_in] "."$list[time_in]'");
$since_start = $start_time->diff(new DateTime("'$list[date_out] "."$list[time_out]'"));

$hours = $since_start->h.' hours';

But it doesn't work. I think my quotes and double quotes are all messed up because the use of " ' . really confuses me..

Thanks in advance for any advice on how I can fix my code!

[EDIT] Thanks everyone for all your detailed help! I just realised that the server doesn't support php 5.3 and I can't use datetime().

So my solution was:

$start_time = strtotime("$list[date_in] " . "$list[time_in]");
$end_time = strtotime("$list[date_out] " . "$list[time_out]");
$hours = abs(($end_time - $start_time)/3600);
Tony Stark
  • 8,064
  • 8
  • 44
  • 63
Cuppy
  • 103
  • 1
  • 1
  • 8
  • why not to have date and time in one `TIMESTAMP` column? – mychalvlcek Feb 18 '13 at 14:22
  • To get hours use dateTime:: format function, if i am writing, I will construt two dates separate ly and will do days diff . This is easy to debug and code is more readable – Shridhar Feb 18 '13 at 14:23
  • Oh I'm not exacly sure.. Still really new at this. I had thought that timestamp was when you had to work with different timezones and I didn't have that constraint. This particular table is sort of like a time log sheet where users can key in the date and time they came in and date and time they left. – Cuppy Feb 18 '13 at 14:40
  • Great! All the answers given by everyone worked perfectly! Thanks a lot! – Cuppy Feb 19 '13 at 13:35

5 Answers5

7

Try this,

function datediff( $date1, $date2 )
{
    $diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );

    return sprintf
    (
        "%d Days, %d Hours, %d Mins, %d Seconds",
        intval( $diff / 86400 ),
        intval( ( $diff % 86400 ) / 3600),
        intval( ( $diff / 60 ) % 60 ),
        intval( $diff % 60 )
    );
}

print datediff( "18th February 2013", "now" ) . "\n";

OR

You can use DateTime::diff

  $start_date = new DateTime("2012-02-10 11:26:00");
    $end_date = new DateTime("2012-04-25 01:50:00");
    $interval = $start_date->diff($end_date);
    echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";

EDIT:

Checkout links,

How to calculate the difference between two dates using PHP?

Php Date Time – 7 Methods to Calculate the Difference between 2 dates.

may help you.

Community
  • 1
  • 1
Tony Stark
  • 8,064
  • 8
  • 44
  • 63
2

You don't need quotes if you want to evaluate the variables content. In this case, you only need them to create a separation between date and time:

$start_time = new DateTime($list['date_in']." ".$list['time_in']);
$since_start = $start_time->diff(new DateTime($list['date_out']." ".$list['time_out']));

I am using the dot . in order to concatenate variables with an string, in this case the string is a white space.

You can read more about concatenation in the documentation.

Alvaro
  • 40,778
  • 30
  • 164
  • 336
1
$start_time = new DateTime("$list[date_in] " . "$list[time_in]");
$since_start = $start_time->diff(new DateTime("$list[date_out] " . "$list[time_out]"));

OR

$start_time = new DateTime("{$list['date_in']} {$list['time_in']}");
$since_start = $start_time->diff(new DateTime("{$list['date_out']} {$list['time_out']}"));
sybear
  • 7,837
  • 1
  • 22
  • 38
1
// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
    // If not numeric then convert texts to unix timestamps
    if (!is_int($time1)) {
      $time1 = strtotime($time1);
    }
    if (!is_int($time2)) {
      $time2 = strtotime($time2);
    }

    // If time1 is bigger than time2
    // Then swap time1 and time2
    if ($time1 > $time2) {
      $ttime = $time1;
      $time1 = $time2;
      $time2 = $ttime;
    }

    // Set up intervals and diffs arrays
    $intervals = array('year','month','day','hour','minute','second');
    $diffs = array();

    // Loop thru all intervals
    foreach ($intervals as $interval) {
      // Set default diff to 0
      $diffs[$interval] = 0;
      // Create temp time from time1 and interval
      $ttime = strtotime("+1 " . $interval, $time1);
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime) {
    $time1 = $ttime;
    $diffs[$interval]++;
    // Create new temp time from time1 and interval
    $ttime = strtotime("+1 " . $interval, $time1);
      }
    }

    $count = 0;
    $times = array();
    // Loop thru all diffs
    foreach ($diffs as $interval => $value) {
      // Break if we have needed precission
      if ($count >= $precision) {
    break;
      }
      // Add value and interval 
      // if value is bigger than 0
      if ($value > 0) {
    // Add s if value is not 1
    if ($value != 1) {
      $interval .= "s";
    }
    // Add value and interval to times array
    $times[] = $value . " " . $interval;
    $count++;
      }
    }

    // Return string with times
    return implode(", ", $times);
}

// Set start & end time
$start_time = "2013-02-18 13:00:00";
$end_time = "2013-02-16 10:00:00";

// Run and print diff
echo dateDiff($start_time, $end_time, 6);

The last argument is the precision.

fnkr
  • 9,428
  • 6
  • 54
  • 61
  • woah, thanks for that! This will take me a while to understand but your comments really helped! I'll try to figure it out and then try to see if it works! – Cuppy Feb 18 '13 at 14:53
0
$diff = abs( strtotime( '2014-04-25 16:00:00' ) - strtotime( '2014-04-27 18:02:00' ) );

if(sprintf("%d",intval( $diff / 86400 )) != '0'){
    if(sprintf("%d",intval( $diff / 86400 )) == '1'){
        echo sprintf("%02d day ", intval( $diff / 86400 ));
    }else{
        echo sprintf("%02d days ", intval( $diff / 86400 ));
    }
}
if(intval( ( $diff % 86400 ) / 3600) != '0'){
    if(intval( ( $diff % 86400 ) / 3600) == '1'){
        echo sprintf("%02d hour ", intval( ( $diff % 86400 ) / 3600));
    }else{
        echo sprintf("%02d hours ", intval( ( $diff % 86400 ) / 3600));
    }
}
if(intval( ( $diff / 60 ) % 60 ) != '0'){
    if(intval( ( $diff / 60 ) % 60 ) == '1'){
        echo sprintf("%02d min", intval( ( $diff / 60 ) % 60 ));
    }else{
        echo sprintf("%02d mins", intval( ( $diff / 60 ) % 60 ));
    }
}
Naresh Chennuri
  • 1,133
  • 11
  • 10