Jump to content


Photo

Remaining Time Between Two Dates?


  • Please log in to reply
6 replies to this topic

#1 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 29 June 2006 - 02:02 PM

I'm trying to calculate the time remaing in days, hour, min, secs between two timestamps but for some reason I'm not getting correct amount of hours? Here is what I have so far.

<? 
$timestamp_future  = time() + (60*60*24*15);
$d = $timestamp_future - time();
print("Current Time: ");
echo(date("M-d-y g:i:s"));
echo("<br>Ends on: ");
print date("M-d-y g:i:s",$timestamp_future);
echo("<br>Remaining: ");
print date("j,g:i:s",$d);
?>

Returns:
Current Time: Jun-29-06 9:55:47
Ends on: Jul-14-06 9:55:47
Remaining: 15,7:00:00 - it is giving me the right amount of days,min,secs but my hours are off

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 02:20 PM

for start, you should know function date return a time since January 1 1970 00:00:00 GMT
so when you do
print date("j,g:i:s",$d);

it return time and date since jan 1 1970 00:00:00 plus $d number of second, so that is exactly what you get.

$d = $timestamp_future - time();
then $d is # of seconds.

you need to convert this into hours, mins, sec like this
$days = (int) $d / 86400; // 86400 is # of seconds in a day (24 * 60 * 60)
$hours = (int) ($d % 86400) / 3600;
$mins = (int) ($d % 3600) / 60;
$seconds = $d % 60;

echo "$days days and $hours:$mins:$seconds";


you might want to do some number formatting to make it look nice.

Life's too short for arguing.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 June 2006 - 02:20 PM

[!--quoteo(post=389258:date=Jun 29 2006, 10:02 AM:name=barkster)--][div class=\'quotetop\']QUOTE(barkster @ Jun 29 2006, 10:02 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I'm trying to calculate the time remaing in days, hour, min, secs between two timestamps but for some reason I'm not getting correct amount of hours? Here is what I have so far.

Returns:
Current Time: Jun-29-06 9:55:47
Ends on: Jul-14-06 9:55:47
Remaining: 15,7:00:00 - it is giving me the right amount of days,min,secs but my hours are off
[/quote]

the date() function takes a valid UNIX timestamp. when you are working with differences, you usually will need to calculate them manually. for instance:
function getDiff($time1, $time2) {
  $diff = abs(strtotime($time1) - strtotime($time2));
  $myArr = array();
  $min = 60;
  $hour = $min * 60;
  $day = $hour * 24;
  $myArr['days'] = floor($diff / $day);
  $diff = $diff % $day;
  $myArr['hours'] = floor($diff / $hour);
  $diff = $diff % $hour;
  $myArr['mins'] = floor($diff / $min);
  $myArr['secs'] = $diff; % $min;
 
  return $myArr;
}

$start = date('Y-m-d h:i:s');
$end = "2006-12-25";
$diff = getDiff($start, $end);
echo "Time until Christmas: $diff[days] Days, $diff[hours] Hours, $diff[mins] Minutes and $diff[secs] Seconds!";

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 29 June 2006 - 02:29 PM

Thanks for the quick reply, I new there was something a bit odd going on and it was me :-) Thanks

#5 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 29 June 2006 - 03:26 PM

Got that working but had a question now, why is my minute always off by one here? I'm trying to add 15 days to a date:

$t = time();
$timestamp_future  = $t + (60*60*24*15);
$t = date("Y-m-d H:m:s",$t);
$timestamp_future = date("Y-m-d H:m:s",$timestamp_future);
echo('<br>'.$t.'<br>'.$timestamp_future);

Returns:
2006-06-29 11:06:15
2006-07-14 11:07:15



Also, how can I go from format "Y-m-d H:m:s" to "M-d-y g:i:s" I thought I could do date("M-d-y g:i:s",$t) but of course it doesn't work.

#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 03:53 PM

$t = time();
$timestamp_future  = $t + (60*60*24*15);
$t = date("Y-m-d H:m:s",$t);  //<<<<you changed $t here
$timestamp_future = date("Y-m-d H:m:s",$timestamp_future);
echo('<br>'.$t.'<br>'.$timestamp_future);


It doesn't work because you changed $t on indicated line.

try this:
$t = time();
$timestamp_future = $t + (60*60*24*15);
echo '<br>' . date("Y-m-d H:m:s",$t); //<<<<you changed $t here
echo '<br>' . date("Y-m-d H:m:s",$timestamp_future);

echo "<br>" . date("M-d-y g:i:s",$t);
Life's too short for arguing.

#7 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 29 June 2006 - 04:15 PM

I knew I did something stupid again, I kept looking at it over and over knowing something was off when I looked at it but kept missing.

When I run this, they future time is still off by one minute??

$t = time();
$timestamp_future = $t + (60*60*24*15);
echo '<br>Current Time: ' . date("Y-m-d H:m:s",$t); //<<<<you changed $t here
echo '<br>Future Time: ' . date("Y-m-d H:m:s",$timestamp_future);
echo "<br>Formatted Future: " . date("M-d-y g:i:s",$t);

Returns:
Current Time: 2006-06-29 12:06:42
Future Time: 2006-07-14 12:07:42
Formatted Future: Jun-29-06 12:18:42




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users