10 Replies - 494 Views - Last Post: 17 January 2009 - 02:25 PM Rate Topic: -----

#1 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

So Stuck

Post icon  Posted 16 January 2009 - 09:08 PM

This is most certainly a beginner question but one I've been having trouble with.

I take two epoch time values and do some math with them to find if its been four hours since an action happened.

I do this like this:

$new_time = $current_time-$original_login;
// Do The Math


Now I have the value as $new_time.

I want to display a static countdown timer on how much time is remaining. I've tried several ways and can't get it working right. Any help? Many Thanks!

My original thinking was just to echo the variable $new_time but just gives me the result of the math... not a human readable format. It will never be over 4 hours if that helps :)

MANY THANKS in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: So Stuck

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,010
  • Joined: 08-August 08

Re: So Stuck

Posted 16 January 2009 - 09:24 PM

Try this:
var_dump(localtime());
echo "<br><br>";
$x = localtime();
echo $x[2].":".$x[1].":".$x[0];


This post has been edited by CTphpnwb: 16 January 2009 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: So Stuck

Posted 16 January 2009 - 11:10 PM

View PostCTphpnwb, on 16 Jan, 2009 - 10:24 PM, said:

Try this:
var_dump(localtime());
echo "<br><br>";
$x = localtime();
echo $x[2].":".$x[1].":".$x[0];



Forgive my noobness I'm still a bit confused. I've come a long way with your help and others on here but I'm a bit confused as to what this will do.
Was This Post Helpful? 0
  • +
  • -

#4 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: So Stuck

Posted 17 January 2009 - 01:38 AM

CT I don't think that is what he was asking at all. I believe he wanted to find out how to check if 4 hours had elapsed?

I'm assuming that you are storing the first time in a MySQL database? More information on this part would be helpful. You must store the time in a database because PHP won't be executing for 4 hours (Hopefully).



Here is how to perform a countdown:

<?php

// Get our data from previously triggered actions:

$query = mysql_query("SELECT time FROM things WHERE something = ".$this."");
$time = mysql_result($query);


// Assign our 4 hour countdown variable:

$countdown = $time + 14400;
$current_time = time();


// Format the time into a readable format:

$time_left = $countdown - $current_time;

$hours = round($time_left / 60 / 60);  // we use round() for obvious math and output reasons
$minutes = round($time_left / 60);
$seconds = substr($time_left, -2);      // substr() gives us the last 2 digits of our time() which is the seconds


// Determine if 4 hours has passed or not:

if ($current_time < $countdown){
	echo "Time left: ".$hours.":".$minutes.":"$seconds."";
} else {
	echo "4 hours has passed!";
}

// Remember to thank: PR4Y

?>






Let me know how everything goes!

This post has been edited by pr4y: 17 January 2009 - 01:48 AM

Was This Post Helpful? 1
  • +
  • -

#5 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 900
  • Joined: 08-August 08

Re: So Stuck

Posted 17 January 2009 - 01:53 AM

i also wrote this code a while ago to give users interest evry 24 hours ill change it to suit you for 4 hours,

just change the table names etc, it has proper english as well so will add an s on the end when it is plural, i dont know if this is the EASIEST way to do it but i know it works

							  $time = mysql_query("select till_interest from members where id ='".$_SESSION['id']."'");
					$time1 = mysql_fetch_array($time);
					$time2 = $time1['till_interest'];// the three lines here give the timestamp given at the start of the 4 hours 
					$time = strtotime($time2);// puts the time into a readable format 
					$timetillinterest = 4;// how many hours 
					$secondstillinterest = (60*60*$timetillinterest);// into seconds
					$timetowait = ceil($secondstillinterest - (mktime() - $time));
					$hoursinseconds = $timetowait/3600;//how many hours in decimal form
					$hourstowait = floor($hoursinseconds);// lowest whole number to the above
					$overflowdec = $hoursinseconds - $hourstowait; // finds the decimal
					$secondsinminutes = $overflowdec*3600;//finds how many seconds over the hour are left
					$minutesremaining = $secondsinminutes/60;//finds how many mins over the hour there are 
					$minutestowait = floor($minutesremaining);// finds the lowest whole number of minutes
					$overflowdecmin = $minutesremaining - $minutestowait;//finds the decimal
					$secondstowait  = $overflowdecmin*60;//works out remaining seconds.
					if($hourstowait != 1){
						$hour = "hours";
					}else{
						$hour = "hour";
					}
					if($minutestowait != 1){
						$minute = "minutes";
					}else{
						$minute = "minute";
					}
					if($secondstowait != 1){
						$second = "seconds";
					}else{
						$second = "second";
					}// the above code will switch hours with hour minuites with minute and seconds with second when the number is 1 
					if($timetowait <= 0 ){
						echo "your interest has payed out";
					}else{
					echo "you have to wait another ".$hourstowait." ".$hour.", ".$minutestowait." ".$minute." and ".$secondstowait." ".$second." ";	
					}

Was This Post Helpful? 0
  • +
  • -

#6 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: So Stuck

Posted 17 January 2009 - 02:07 AM

Sloppy code!

</off topic>
Was This Post Helpful? 0
  • +
  • -

#7 RayRayAngel  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 15-December 08

Re: So Stuck

Posted 17 January 2009 - 02:47 AM

Well here is what I'm using:

$countdown = ($time+14400);
$time_left = ($countdown-$current_time);
$hours = round($time_left / 60 / 60);
$minutes = round($time_left / 60);
$seconds = substr($time_left - 2);
if ($current_time<$countdown){
	 echo "Time Remaining Before Next Bonus: <strong>";
	 echo $hours . " hours " . $minutes . " minutes " . $seconds . " seconds";
	 } else {
	 echo "4 hours has passed!";
}
echo "</strong>";



And here is the result I'm getting....
Time Remaining Before Next Bonus: 3 hours 194 minutes seconds

So actually its working for the most part. Minutes and seconds seems a bit off. Trying to track down why.

Thank you all for the wonderful examples!

View Postpr4y, on 17 Jan, 2009 - 02:38 AM, said:

CT I don't think that is what he was asking at all. I believe he wanted to find out how to check if 4 hours had elapsed?

I'm assuming that you are storing the first time in a MySQL database? More information on this part would be helpful. You must store the time in a database because PHP won't be executing for 4 hours (Hopefully).



Here is how to perform a countdown:

<?php

// Get our data from previously triggered actions:

$query = mysql_query("SELECT time FROM things WHERE something = ".$this."");
$time = mysql_result($query);


// Assign our 4 hour countdown variable:

$countdown = $time + 14400;
$current_time = time();


// Format the time into a readable format:

$time_left = $countdown - $current_time;

$hours = round($time_left / 60 / 60);  // we use round() for obvious math and output reasons
$minutes = round($time_left / 60);
$seconds = substr($time_left, -2);      // substr() gives us the last 2 digits of our time() which is the seconds


// Determine if 4 hours has passed or not:

if ($current_time < $countdown){
	echo "Time left: ".$hours.":".$minutes.":"$seconds."";
} else {
	echo "4 hours has passed!";
}

// Remember to thank: PR4Y

?>






Let me know how everything goes!

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,010
  • Joined: 08-August 08

Re: So Stuck

Posted 17 January 2009 - 06:05 AM

View PostRayRayAngel, on 16 Jan, 2009 - 10:10 PM, said:

View PostCTphpnwb, on 16 Jan, 2009 - 10:24 PM, said:

Try this:
var_dump(localtime());
echo "<br><br>";
$x = localtime();
echo $x[2].":".$x[1].":".$x[0];



Forgive my noobness I'm still a bit confused. I've come a long way with your help and others on here but I'm a bit confused as to what this will do.

Sorry, I was tired and thought that you could figure it out from there. The point was that since local time gives you the time in an array ($x[2] = hours, $x[1] = minutes, and $x[0] = seconds) you could use that to calculate time differences.
Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 900
  • Joined: 08-August 08

Re: So Stuck

Posted 17 January 2009 - 06:51 AM

i see the problem, what your doing for minutes is how many minutes are left TOTALLY not just over the hour

try adding
$countdown = ($time+14400);
$time_left = ($countdown-$current_time);
$hours = round($time_left / 60 / 60);
$minutes = (round($time_left / 60))-$hours*60;
$seconds = round($time_left)-($hours*60*60)-($minutes*60);
if ($current_time<$countdown){
	 echo "Time Remaining Before Next Bonus: <strong>";
	 echo $hours . " hours " . $minutes . " minutes " . $seconds . " seconds";
	 } else {
	 echo "4 hours has passed!";
}
echo "</strong>";
 

Was This Post Helpful? 0
  • +
  • -

#10 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: So Stuck

Posted 17 January 2009 - 07:50 AM

For your time display, you could try something like this. I wrote this for a script I use that handles duration of operations for a site I'm writing. I've adjusted the conditionals to use your variables.

$hours = $minutes = $seconds = 0;
		while($time_left > 0) {
			while($time_left >= 60) {
				while($time_left >= 3600) {
					$hours++;
					$time_left -= 3600;
				}
				$minutes++;
				$time_left -= 60;
			}
			$seconds++;
			$time_left -= 1;
		}
		
		if($hours > 0) {
			$duration = "{$hours} hour";
			$duration .= ($hours > 1) ? "s" : "";
			if($minutes > 0) {
				if($seconds > 0) {
					$duration .= ", {$minutes} minute";
					$duration .= ($minutes > 1) ? "s" : "";
					$duration .= ", and {$seconds} second";
					$duration .= ($seconds > 1) ? "s" : "";
				}
				else if($seconds > 0) {
					$duration .= " and {$seconds} second";
					$duration .= ($seconds > 1) ? "s" : "";
				}
			}
		}
		else if($minutes > 0) {
			$duration = "{$minutes} minute";
			$duration .= ($minutes > 1) ? "s" : "";
			if($seconds > 0) {
				$duration .= " and {$seconds} second";
				$duration .= ($seconds > 1) ? "s" : "";
			}
		}
		else if($seconds > 0) {
			$duration = "{$seconds} second";
			$duration .= ($seconds > 1) ? "s" : "";
		}
		else {
			$duration = "None specified";
		}


After running that code, simply echo $duration where you want the remaining time to be :)
Was This Post Helpful? 0
  • +
  • -

#11 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: So Stuck

Posted 17 January 2009 - 02:25 PM

Thanks for the corrections on my algorythm, it was 3:45 AM last night when I wrote that :-P


Glad to see its working for you now though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1