Jump to content

Working out time difference


MasterACE14

Recommended Posts

hey,

 

I have a column which is updated with time(); in the database every 30mins.

 

I want to find out how many minutes and seconds are between now and then.

 

What I have in the moment is close, but not quite right.

 

<?php
    $lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins
   $currentTime = time(); // current time
   $nextTurnMin = date("i",($currentTime)) - date("i",round($lastTurn)); // work out the difference

 

any help is greatly appreciated

 

Regards ACE

Link to comment
Share on other sites

it's soughta working lol.

 

This is a countdown timer until my cron which will update LastTurnTime.

 

so I have this in the moment:

<?php
$diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin); // $nextTurnMin is how many minutes till the next update(which happen every half an hour)
$mins = ($diff % 60);
$secs = ($diff - $mins);

 

been experimenting for hours trying to get this right.

 

I'm basically making the count down livein javascript, which is working. Just the current Server Time, doesn't match up with the current count down. Like the closest I have got it is within 2 seconds of being the same.

 

I have...

<?php print date("M j, H:i:s", ($currentTime)); // $currentTime is time(); , this is also a block of javascript ?>

which is showing the current server time.

 

and this is showing the countdown...

<script language="javascript">
<!--
var sec = <?php echo $secs; ?>;   // set the seconds
var min = <?php echo $mins; ?>;   // set the minutes

function countDown() {
  sec--;
  if (sec == -01) {
    sec = 59;
    min = min - 1;
  } else {
   min = min;
  }
if (sec<=9) { sec = "0" + sec; }
  time = (min<=9 ? "0" + min : min) + ":" + sec + " min ";
if (document.getElementById) { theTime.innerHTML = time; }
  SD=window.setTimeout("countDown();", 1000);
if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); time = "Now!"; }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
-->
</script>

 

in the moment the countdown timer is looking like this...

0-22:43 min

until next Update

 

while the server time is about 4 minutes ahead.

Link to comment
Share on other sites

Hi

 

Would be tempted to do it in the MySQL call that gets the last time updated.

 

I presume LastTurnTime is an array populates from a datetime field on the database.

 

If so use SELECT (TIMESTAMPDIFF(SECOND, NOW(), LastTurnTime) as TimeSinceLastTurnTime to get the difference in seconds.

 

All the best

 

Keith

Link to comment
Share on other sites

here is my javascript countdown script. the things in {} need setting to what you want.

 

<script type="text/javascript">
function countdown{cid}(){
document.getElementById('countdown{cid}').innerHTML = ((document.getElementById('countdown{cid}').innerHTML * 1) - 1);
if(document.getElementById('countdown{cid}').innerHTML > 0){
	var s = document.getElementById('countdown{cid}').innerHTML % 60;
	var m = ((document.getElementById('countdown{cid}').innerHTML - s) / 60) % 60;
	var h = ((((document.getElementById('countdown{cid}').innerHTML - s) / 60) - m) / 60) % 24;
	var d = ((((((document.getElementById('countdown{cid}').innerHTML - s) / 60) - m) / 60) - h) / 24);

	var x = '';
	if( d > 0 ){ x = x + " "+d+"d"; }
	if( h > 0 ){ x = x + " "+h+"h"; }
	if( m > 0 ){ x = x + " "+m+"m"; }
	if( s > 0 ){ x = x + " "+s+"s"; }
}else{
	var x = '{cfin}';
}

document.getElementById('countdown{cid}p').innerHTML = x;
var t=setTimeout("countdown{cid}()",1000);
}
countdown{cid}();
</script>
<div id="countdown{cid}" style="display:none;">{cstart}</div><span id="countdown{cid}p"></span>

 

{cid} is the id of the countdown, it is if you have more than 1 countdown on the page.

{cstart} is how many seconds there are

{cfin} is what to say when the time has been reached.

 

you should add into the body

 

<body onload="countdown{cid}();">

Link to comment
Share on other sites

I got nothing coming up now lol. Think I did something wrong?

<?php
$diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin);
$mins = ($diff % 60);
$NextTurn = $mins * 60;
?>

<script type="text/javascript">
function countdown1() {
document.getElementById('countdown1').innerHTML = ((document.getElementById('countdown1').innerHTML * 1) - 1);
if(document.getElementById('countdown1').innerHTML > 0){
	var s = document.getElementById('countdown1').innerHTML % 60;
	var m = ((document.getElementById('countdown1').innerHTML - s) / 60) % 60;
	var h = ((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) % 24;
	var d = ((((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) - h) / 24);

	var x = '';
	if( d > 0 ){ x = x + " "+d+"d"; }
	if( h > 0 ){ x = x + " "+h+"h"; }
	if( m > 0 ){ x = x + " "+m+"m"; }
	if( s > 0 ){ x = x + " "+s+"s"; }
}else{
	var x = 'Now!';
}

document.getElementById('countdown1p').innerHTML = x;
var t=setTimeout("countdown1()",1000);
}
countdown1();
</script>
<div id="countdown1" style="display:none;"><?php echo $NextTurn; ?></div><span id="countdown1p"></span>

 

Ah, so not a datetime field. Oh well. What format is it in?

 

All the best

 

Keith

 

it's integer, I insert the time(); function into it with PHP.

Link to comment
Share on other sites

Hi

 

Right, you are storing a number of seconds (ie, could have been minutes).

 

Doing it in php:-

 

    $lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins
   $currentTime = time(); // current time
   $nextTurnSecs = (time() - $lastTurndate); // work out the difference
   $nextTurnMin = floor($nextTurnSecs/60); // work out the difference
   $nextTurnSecs = $nextTurnSecs - ($nextTurnMin * 60); // work out the difference

 

Not tried it but think that will work and give you the minutes and seconds since the time, in a pair of fields.

 

All the best

 

Keith

Link to comment
Share on other sites

I got nothing coming up now lol. Think I did something wrong?

<?php
$diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin);
$mins = ($diff % 60);
$NextTurn = $mins * 60;
?>

<script type="text/javascript">
function countdown1() {
document.getElementById('countdown1').innerHTML = ((document.getElementById('countdown1').innerHTML * 1) - 1);
if(document.getElementById('countdown1').innerHTML > 0){
	var s = document.getElementById('countdown1').innerHTML % 60;
	var m = ((document.getElementById('countdown1').innerHTML - s) / 60) % 60;
	var h = ((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) % 24;
	var d = ((((((document.getElementById('countdown1').innerHTML - s) / 60) - m) / 60) - h) / 24);

	var x = '';
	if( d > 0 ){ x = x + " "+d+"d"; }
	if( h > 0 ){ x = x + " "+h+"h"; }
	if( m > 0 ){ x = x + " "+m+"m"; }
	if( s > 0 ){ x = x + " "+s+"s"; }
}else{
	var x = 'Now!';
}

document.getElementById('countdown1p').innerHTML = x;
var t=setTimeout("countdown1()",1000);
}
countdown1();
</script>
<div id="countdown1" style="display:none;"><?php echo $NextTurn; ?></div><span id="countdown1p"></span>

 

Ah, so not a datetime field. Oh well. What format is it in?

 

All the best

 

Keith

 

it's integer, I insert the time(); function into it with PHP.

 

 

 

$diff = ceil(LastTurnTime($userrow['ID'])) - ceil($nextTurnMin);

why are you ceil'ing an integer? ciel rounds up to the next integer, time() is already an integer?

 

also what exactly are you trying to do? Your script is using variables and you aren't defining them.

 

What do you want the time until and how is it worked out?

 

 

Link to comment
Share on other sites

<?php
$lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins
   $currentTime = time(); // current time
   $nextTurnSecs = (time() - $lastTurndate); // work out the difference
   $nextTurnMin = floor($nextTurnSecs/60); // work out the difference
   $nextTurnSecs = $nextTurnSecs - ($nextTurnMin * 60); // work out the difference
?>

<script language="javascript">
<!--
var sec = <?php echo $nextTurnMin; ?>;   // set the seconds
var min = <?php echo $nextTurnSecs; ?>;   // set the minutes

function countDown() {
  sec--;
  if (sec == -01) {
    sec = 59;
    min = min - 1;
  } else {
   min = min;
  }
if (sec<=9) { sec = "0" + sec; }
  time = (min<=9 ? "0" + min : min) + ":" + sec + " min ";
if (document.getElementById) { theTime.innerHTML = time; }
  SD=window.setTimeout("countDown();", 1000);
if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); time = "Now!"; }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
-->
</script>

<span id="theTime" class="timeClass"></span>

 

That javascripts working.

 

But the time until Next Turn is showing up as... 52:6316436 <<< time(); I believe

Link to comment
Share on other sites

Hi

 

Had a play and think I have got it to work:-

 

<?php
$lastTurn = LastTurnTime($userrow['ID']); // time(); updated in database every 30 mins
$currentTime = time(); // current time
$nextTurnSecs = 1800 - (($currentTime - $lastTurn)); // work out the difference
$nextTurnMin = floor($nextTurnSecs/60); // work out the difference
$nextTurnSecs = $nextTurnSecs - ($nextTurnMin * 60); // work out the difference
?>

<script language="javascript">
<!--
var sec = <?php echo $nextTurnSecs; ?>;   // set the seconds
var min = <?php echo $nextTurnMin; ?>;   // set the minutes
addLoadEvent = countDown();

function countDown() 
{
sec--;
if (sec == -01) 
{
	sec = 59;
	min = min - 1;
}
else
{
	min = min;
}
if (sec<=9) 
{ 
	sec = "0" + sec; 
}
var  time = (min<=9 ? "0" + min : min) + ":" + sec + " min ";
if (document.getElementById('$currentTime')) 
{ 
	document.getElementById('$currentTime').innerHTML = time; 
}
SD=window.setTimeout("countDown();", 1000);
if (min == '00' && sec == '00') 
{ 
	sec = "00"; 
	window.clearTimeout(SD); 
	time = "Now!"; 
}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
-->
</script>

 

Primary error was the Javascript minutes and seconds being the wrong way round. Also the calculation at the top calculated the number of seconds since last updated, while you wanted to count down from that until 30 minutes later (hence now taking the number of seconds since last updated away from 1800).

 

All the best

 

Keith

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.