• 0

How to calculate duration in PHP


Question

5 answers to this question

Recommended Posts

  • 0

Yours won't work ;)

Use something like:

$start = mktime();

...

//later in the script
$now = mktime();

$duration = $now - $start;

This leaves you with an answer in milliseconds, so just do the maths on it to convert into any string you want.

Link to comment
Share on other sites

  • 0

Yours won't work ;)

Use something like:

$start = mktime();

...

//later in the script
$now = mktime();

$duration = $now - $start;

This leaves you with an answer in milliseconds, so just do the maths on it to convert into any string you want.

Why don't you read the question again.

He is not trying to measure the time of execution of his script.

Link to comment
Share on other sites

  • 0

<?

$date = "2005-12-01";
$today = date("Ymd");

$date_exp = explode("-",$date);
$date_recon = $date_exp[0].$date_exp[1].$date_exp[2];

$duration = $today - $date_recon;

echo $duration;

?>

Just takes the hypens out of the date to compare it to, and puts todays date in the same yyyymmdd.

Link to comment
Share on other sites

  • 0

Try this..

#---------------------------------------------------------------------------------------
# FUNCTION: datediff($interval, $datefrom, $dateto, $using_timestamps = false)
# DATE CREATED: Mar 31, 2005
# AUTHOR: I Love Jack Daniels
# PURPOSE: Just like the DateDiff function found in Visual Basic
#	$interval can be:
#	yyyy   - Number of full years
#	q	  - Number of full quarters
#	m	  - Number of full months
#	y	  - Difference between day numbers  (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
#	d	  - Number of full days
#	w	  - Number of full weekdays
#	ww	 - Number of full weeks
#	h	  - Number of full hours
#	n	  - Number of full minutes
#	s	  - Number of full seconds (default)
#---------------------------------------------------------------------------------------
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {  
  if (!$using_timestamps) {
	$datefrom = strtotime($datefrom, 0);
	$dateto = strtotime($dateto, 0);
  }
  $difference = $dateto - $datefrom; // Difference in seconds
  switch($interval) {
	case 'yyyy': $years_difference = floor($difference / 31536000);
				if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
					$years_difference--;
				}
				if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
					$years_difference++;
				}
				$datediff = $years_difference;
				break;
	case "q":   $quarters_difference = floor($difference / 8035200);
				while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
					$months_difference++;
				}
				$quarters_difference--;
				$datediff = $quarters_difference;
				break;
	case "m":   $months_difference = floor($difference / 2678400);
				while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
					$months_difference++;
				}
				$months_difference--;
				$datediff = $months_difference;
				break;
	case 'y':   $datediff = date("z", $dateto) - date("z", $datefrom); break;
	case "d":   $datediff = floor($difference / 86400); break;
	case "w":   $days_difference = floor($difference / 86400);
				$weeks_difference = floor($days_difference / 7); // Complete weeks
				$first_day = date("w", $datefrom);
				$days_remainder = floor($days_difference % 7);
				$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
				if ($odd_days > 7) { $days_remainder--; }
				if ($odd_days > 6) { $days_remainder--; }
				$datediff = ($weeks_difference * 5) + $days_remainder;
				break;
	case "ww":  $datediff = floor($difference / 604800); break;
	case "h":   $datediff = floor($difference / 3600); break;
	case "n":   $datediff = floor($difference / 60); break;
	default:	$datediff = $difference; break;
  }	
  return $datediff;
}

I also find this one useful..

#---------------------------------------------------------------------------------------
# FUNCTION: timeAgo($datefrom, $using_timestamps = false)
# DATE CREATED: Mar 31, 2005
# AUTHOR: M. Shepanski
# PURPOSE: Returns a nice time interval string with the amount of time that has past
#		  between the current time, and the time sent in.
#---------------------------------------------------------------------------------------
function timeAgo($datefrom, $using_timestamps = false) {
	if (!$using_timestamps) { $datefrom = strtotime($datefrom, 0); }		// Convert time to timestamp
	$difference = time() - $datefrom;									   // Difference in seconds
	if	 ($difference < 120)	   { $rtnval = $difference." sec ago"; }
	elseif ($difference < 7200)	  { $rtnval = round(($difference / 60), 0)." min ago"; }
	elseif ($difference < 172800)	{ $rtnval = round(($difference / 3600), 0)." hrs ago"; }
	elseif ($difference < 168739200) { $rtnval = round(($difference / 86400), 0)." days ago"; }
	else							 { $rtnval = "NA"; }
	return $rtnval;
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.