[HELP][PHP/MySQL]Math Problem , please help asap.

Results 1 to 8 of 8
  1. #1
    Member ^^Exerct is offline
    MemberRank
    Jan 2009 Join Date
    77Posts

    [HELP][PHP/MySQL]Math Problem , please help asap.

    Hi, im working on a game and currently im coding a jail.

    When you get arrested it captures the current time in HourMinuteSecond format so if the time was 06:13:23 when arrested it would store it in a sql database as 061323, and it would also take that and add a the jail time to it. So lets say you get arrested for 15 minutes it takes the variable wich 061323 is in and adds 60*15 to get 15 minutes and then it stores that too in the sql database.

    So now i have 2 values, the time of arrest and the time of release this means i can get the release function to work but i want to have a countdown timer so the userse can see the time left.

    I want so every time you refresh the page the new timeleft appears, i have no idea how to calculate the timeleft so therefor im asking here.

    EDIT: FIXED, IF U HAVE THE SAME PROBLEM LOOK AT POST #5!!
    Last edited by ^^Exerct; 16-07-10 at 12:05 AM.


  2. #2
    All is well... Wh005h is offline
    MemberRank
    Feb 2006 Join Date
    TejasLocation
    1,984Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    My suggestion is to use time() like everyone else. This is your best bet.
    Read up on it and you'll see why its awesome =)

  3. #3
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    You should store days, too, else it will go wrong when the day changes.

    time left = time - (now - arrest)

    Usually times are stored and processed in the long format (seconds passed since 1970) when we're only interested in absolute times and not dates or anything, because then calculations need no conversions between days, hours, minutes and seconds. The above, too, you could calculate directly as it stands.

  4. #4
    Member ^^Exerct is offline
    MemberRank
    Jan 2009 Join Date
    77Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    Ok thanks for the quick reply i will test and come back with either an edit or a new reply.

    EDIT: Didnt see teh frist reply, i use date, idk the difference.

    EDIT: Negata, that did not work :/

    Time of Arrest: 629401
    Time of Release: 635401
    Outcome: 626407

    That basicly counts up instead of down.
    Last edited by ^^Exerct; 15-07-10 at 06:36 AM.

  5. #5
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    Quote Originally Posted by ^^Exerct View Post
    Ok thanks for the quick reply i will test and come back with either an edit or a new reply.

    EDIT: Didnt see teh frist reply, i use date, idk the difference.

    EDIT: Negata, that did not work :/

    Time of Arrest: 629401
    Time of Release: 635401
    Outcome: 626407

    That basicly counts up instead of down.
    With time I meant the time passed since arrest. Well if you already have release, then simply do release - now. But you can't do this in the hhmmss format directly, so I really advice you to process your times in the long format.

    Don't tell me it doesn't work, you're doing it wrong. :)

    ---------- Post added at 07:48 AM ---------- Previous post was at 07:41 AM ----------

    I don't do PHP, but based on the docs I assume this would work:
    PHP Code:
    // When arrested
    $arrest time();

    $jailTime 15//minutes

    $release $arrest $jailTime 60;

    // On page update
    $timeLeft $release time();

    $timeString date("H:i.s"$timeLeft); 

  6. #6
    Valued Member Sifex is offline
    MemberRank
    Jul 2010 Join Date
    AustraliaLocation
    139Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    PHP Code:
    // Run script on Arrest (once)
    $arrestTime time(); // Put into MySQL

    $jailTime 15//minutes 

    $release $arrest $jailTime 60//60 Sec * 15 = 15 minutes (Also into MySQL) 
    I personally wouldn't put a countdown timer, just a mintue updater (ie 5 min left)
    PHP Code:
    // Run every mintue
    funtion timeCheck() {
    $timeLeft $release time(); 
    if(
    $timeLeft==0) {release();} else { echo "You have " $timeLeft " minutes remaining...";}

    Dont put it into mysql as a number
    Last edited by Sifex; 15-07-10 at 07:49 AM.

  7. #7
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    Quote Originally Posted by Sifex View Post
    I personally wouldn't put a countdown timer, just a mintue updater (ie 5 min left)
    PHP Code:
    // Run every mintue
    funtion timeCheck() {
    $timeLeft $release time(); 
    if(
    $timeLeft==0) {release();} else { echo "You have " $timeLeft " minutes remaining...";}

    Dont put it into mysql as a number
    $timeLeft is time in seconds.

    Generally it's not a good idea to use polling design. Rather than updating every minute, make the check when needed, like when the user actually tries to make an action. A countdown timer could be implemented on the browser side (JavaScript?).

  8. #8
    Member ^^Exerct is offline
    MemberRank
    Jan 2009 Join Date
    77Posts

    Re: [HELP][PHP/MySQL]Math Problem , please help asap.

    Quote Originally Posted by Negata View Post
    With time I meant the time passed since arrest. Well if you already have release, then simply do release - now. But you can't do this in the hhmmss format directly, so I really advice you to process your times in the long format.

    Don't tell me it doesn't work, you're doing it wrong. :)

    ---------- Post added at 07:48 AM ---------- Previous post was at 07:41 AM ----------

    I don't do PHP, but based on the docs I assume this would work:
    PHP Code:
    // When arrested
    $arrest time();

    $jailTime 15//minutes

    $release $arrest $jailTime 60;

    // On page update
    $timeLeft $release time();

    $timeString date("H:i.s"$timeLeft); 
    Thanks, i will try when i have time.
    Quote Originally Posted by Sifex View Post
    PHP Code:
    // Run script on Arrest (once)
    $arrestTime time(); // Put into MySQL

    $jailTime 15//minutes 

    $release $arrest $jailTime 60//60 Sec * 15 = 15 minutes (Also into MySQL) 
    I personally wouldn't put a countdown timer, just a mintue updater (ie 5 min left)
    PHP Code:
    // Run every mintue
    funtion timeCheck() {
    $timeLeft $release time(); 
    if(
    $timeLeft==0) {release();} else { echo "You have " $timeLeft " minutes remaining...";}

    Dont put it into mysql as a number
    Will probably do this as a last resort.
    Quote Originally Posted by Negata View Post
    $timeLeft is time in seconds.

    Generally it's not a good idea to use polling design. Rather than updating every minute, make the check when needed, like when the user actually tries to make an action. A countdown timer could be implemented on the browser side (JavaScript?).
    Good to know.

    Ill come with an edit or a new reply telling if it worked or not.
    EDIT: It worked, thanks.
    Last edited by ^^Exerct; 16-07-10 at 12:06 AM.



Advertisement