Jump to content

need help with timing


sooty2006

Recommended Posts

Hi, im buiding a website and users can buy credits with these credits they can buy membership.I want to show on the users control panel how long they have left before there mebership ends.I have my own scripts which works up to 24 hours but no futher.They can only register for 30 days, can anyone provide me with the script or tell me what i neeed to do?Heres the script for the 24 hour timer.

function timerfunc24h($secs) {  if($secs > 86400) { $hours = $secs / 86400; $mins = $secs % 86400 / 60; $secs = $secs % 60; 	echo (int)$hours;	echo " Hours and ";	if((int)$mins < 10) {	  echo "0";	}	echo (int)$mins; 	echo " Minutes and ";	if((int)$secs < 10) {	  echo "0";	}	echo (int)$secs;	echo " Seconds";   }elseif($secs > 60) { $mins = $secs / 60; $secs = $secs % 60; 	if((int)$mins < 10) {	  echo "0";	}	echo (int)$mins; 	echo " Minutes and ";	if((int)$secs < 10) {	  echo "0";	}	echo (int)$secs;	echo " Seconds";  }else{ $secs = $secs % 60; 	if((int)$secs < 10) {	  echo "0";	}	echo (int)$secs;	echo " Seconds"; }  }

This would output something like:23 Hours and 37 Minutes and 24 Seconds.I would like to output:16 Days and 23 Hours and 37 Minutes and 24 Seconds.Thanks.

Link to comment
Share on other sites

I believe that the PHP date function already does that for you:

function timerfunc($seconds) {  echo	  date('d',$seconds), ' days and ',	  date('H',$seconds), ' hours and ',	  date('i', $seconds), ' minutes and ',	  date('s',$seconds), ' seconds.';}

Link to comment
Share on other sites

I believe that the PHP date function already does that for you:
function timerfunc($seconds) {  echo	  date('d',$seconds), ' days and ',	  date('H',$seconds), ' hours and ',	  date('i', $seconds), ' minutes and ',	  date('s',$seconds), ' seconds.';}

this does work but if i set the time for too 2592000 seconds which is equal to 30 daysthe time starts with 32 days when i get down to 2 days left it tells me theres 30 days left again?how do i fix this?
Link to comment
Share on other sites

Hmm, it actually shouldn't do that, but the date method wouldn't be the best way to deal with this anyways. Because it doesn't actually count, it gives dates. I wasn't thinking when I suggested it, but there would be other mitakes.If I thought it out correctly, this should give you the right answer:You'll first have to divide by (24*3600) and get the floor() of the result to get the amount of days, then get the modulo of that same division and divide it by 3600 to get the hours, get the modulo of that other division and divide it by 60 to get the minutes and finally, the modulo of that division would be the seconds.$d = floor($seconds / (24*3600);$h = floor( ($seconds%(24*3600)) / 3600);$m = floor( ( ( $seconds%(24*3600) ) % 3600 ) / 60);$s = ( $seconds%(24*3600) ) % 3600 ) % 60;With a little thinking there's probably a way to simplify this a bit.

Link to comment
Share on other sites

Hmm, it actually shouldn't do that, but the date method wouldn't be the best way to deal with this anyways. Because it doesn't actually count, it gives dates. I wasn't thinking when I suggested it, but there would be other mitakes.If I thought it out correctly, this should give you the right answer:You'll first have to divide by (24*3600) and get the floor() of the result to get the amount of days, then get the modulo of that same division and divide it by 3600 to get the hours, get the modulo of that other division and divide it by 60 to get the minutes and finally, the modulo of that division would be the seconds.$d = floor($seconds / (24*3600);$h = floor( ($seconds%(24*3600)) / 3600);$m = floor( ( ( $seconds%(24*3600) ) % 3600 ) / 60);$s = ( $seconds%(24*3600) ) % 3600 ) % 60;With a little thinking there's probably a way to simplify this a bit.
i have tried this and it now does minus figures i think there may be an error in the scripting but im not to sure ive never used the floor function before!
Link to comment
Share on other sites

Divisions and multiplications can't give negative numbers unless you gave them a negative number to begin with. Maybe you can show the code you have. Floor() simply removes the decimal part of a floating point number.

Link to comment
Share on other sites

$query = mysql_query("SELECT * FROM accounts WHERE email='$user_email'");$array = mysql_fetch_array($query);$elf = $array['paid_for'];$result = @mysql_query("SELECT paid_time FROM accounts WHERE email='$user_email'");$time = mysql_fetch_array($result);$now = time(U);$time_diff = $now - $time[0];$time_left = $elf - $time_diff;$tottime = $time[0] - time(U);if ($time_diff < $elf) {echo "Not a member!";} else {datesectime($time_left);}

this will output how many seconds are left then i would use the function to output the seconds into hours etc...and the function is as i have wrote at the top of this topic.Thanks

Link to comment
Share on other sites

i have used your opposed method it outputs a minus figure, I output $time_left and was never given a minus figure unless the time went past 0. Also i dont know if anyone knows but i carnt use the reply function in Firefox the textarea wont let me type i have to use IE to respond

Link to comment
Share on other sites

i have figuered it out 30 days in seconds is 2592000 - sum == seconds left i set the time paid form in seconds on my mysql database for some unknow reason when i was getting it from the database it was outputing a complete different number - wierd?? but thanks for your help i probably would of given up if it wasnt for the help you have given me! i am using your first code using the date(); function and it output exactly what i need cheers mate!

Link to comment
Share on other sites

Hmm, the only problem with the date function is that if it's more than 31 days it's going to start counting back from 1 again. And it's always going to show one more day than there should be.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...