0
Can any one help with a simple php code to get time difference

this is what i did, don't be mad at my effort, i tryed other ways and failed.
$tf_con =time(st$_POST['time_out'] - $_POST['time_in']);
4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by accra
0
<?
$tf_con=@strtotime(@$_POST['time_out'])-@strtotime(@$_POST['time_in']);
echo("[".$tf_con."] seconds or<br/>");
$tf_con/=60;echo("[".$tf_con."] minutes or<br/>");
$tf_con/=60;echo("[".$tf_con."] hours or<br/>");
$tf_con/=24;echo("[".$tf_con."] days<br/>");
?>
<html>
  <body>
    <form method='POST'>
      time_out (YYYY/MM/DD HH:MM:SS): <input type='text' name='time_out' value="<?echo(@$_POST['time_out']);?>"/>
      time_in (YYYY/MM/DD HH:MM:SS): <input type='text' name='time_in' value="<?echo(@$_POST['time_in']);?>"/>
      <input type='submit' value='Submit'/>
    </form>
  </body>
</html>
0

Use the below function it will give you the time difference for both dates and UNIX timestamps

<?php
 
  // Set timezone
  date_default_timezone_set("UTC");
 
  // 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);
  }
 
?>

dateDiff function example usage

echo dateDiff("2010-01-26", "2004-01-26") . "\n";
echo dateDiff("2006-04-12 12:30:00", "1987-04-12 12:30:01") . "\n";
echo dateDiff("now", "now +2 months") . "\n";
echo dateDiff("now", "now -6 year -2 months -10 days") . "\n";
echo dateDiff("2009-01-26", "2004-01-26 15:38:11") . "\n";

Output

6 years
18 years, 11 months, 30 days, 23 hours, 59 minutes, 59 seconds
2 months
6 years, 2 months, 10 days
4 years, 11 months, 30 days, 8 hours, 21 minutes, 49 seconds
0

DO we know the format of the

st$_POST['time_out'] - $_POST['time_in']

What's the 'st' anyway?

Is this in H:i or H:i:s or are both times timestamps - which I assume they'd have to be if you try to substract them.

0

Thanks to you all for your time,am grateful.

Am trying them out.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.