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
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

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.