Jump to content

date and time countdown - Help please!


robcrozier

Recommended Posts

Hi,

 

what i'm trying to do is display the duration between two dates in a similar format to that seen on eBay lots.  For example 1d, 2h, 23m.

 

I have some code already that does most of the job, here it is:


function dateDiff($from,$to) 
{
  $diff = $to - $from;
  $info = array();
  if($diff>86400) {
	//one or more days
	$info['d'] = ($diff - ($diff%86400))/86400;
	$diff = $diff%86400;
  }
  if($diff>3600) {
	//one or more hours
	$info['h'] = ($diff - ($diff%3600))/3600;
	$diff = $diff%3600;
  }
  if($diff>60) {
	//one or more minutes
	$info['m'] = ($diff - ($diff%60))/60;
	$diff = $diff%60;
  }
  else {
	//less than 1 min
	 return $diff."s";
  }
  $f = '';
  foreach($info as $k=>$v) {
	if($v>0) $f .= "$v$k, ";
  }
  return substr($f,0,-2);
}

 

However....  the problem occurs with the number of hours that it displays.  It seems to add 12 hours to the display.  For example if there is 1h, 20m left, this function displays 13h, 20!

 

Can anyone suggest why this is?  I'm pulling a datetime field strain from mysql and converting it to timestamp using strtotime() if this helps?

 

I have a js function that does the same thing using the same datetime values (only difference being that it is a live counter) and it works fine?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/90547-date-and-time-countdown-help-please/
Share on other sites

that seems like a lot of code. can't you just do:

<?php

$date_in_past = strtotime("2008-01-31 08:53:22");
$cur_date = time();
$time_passed = $cur_date - $date_in_past; // number of seconds between two dates

$elapsed_time = gmdate("d\d, h\h, i\m",$time_passed);  // format those seconds from an epoch date.

print "$elapsed_time";

?>

 

note, i used gmdate() instead of date() to avoid the timezone shift.  which could be why your script is off by 12 hours

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.