Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:33:52+00:00 2026-05-29T23:33:52+00:00

i have a situation where i need to calculate the remaining time every 6

  • 0

i have a situation where i need to calculate the remaining time every 6 hours, whenever i view the time.

i have this setup:

<div id="time"><div>
<button>trigger</button>

to be more precise i have a trigger that gets the time starter:

$(buttom).on('click', function(){
    ..... send through ajax the current time to a php file
    ...if success, get the response and place it in the div
});

in the php file i store that time to the database

if (isset($_POST['time'])){
    $storeTime->timestore($_POST['time']);
}

what happens now is that whenever i view that div i should see the left time:

<div id="time">5h:50min<div>

i vizit again in 30 min i see

<div id="time">5h:20min<div>

and so on.

the problem is not sending the time back and forth using ajax or something, but sending the correct time.

What i was thinking is to send the time every time i visit the page. First time store it in a table field and the other times to store them in a separate table field

id     time1        time2
1      123456..     123124..

the time1 stays unmodified as it is the original time and every time i visit the page i send the new current time and update time2

here i get a bit lost.

this is how i get time1: $getTime = $data->getTime($userId);

and this is the time that comes in every time: $time

i also know that 6h is 21600 seconds

so

if ( $time >= ($newTime + 21600) ){ 
    //if the current time is bigger than the first time + 6h it means that 6h have passed
    // store the new time in the database for reference as the new main time
} else {
    // 6h have not passed yet and we need to calculate how much time is left
    //here i get confuzed

}

I know this post is a bit confuse maybe, but i hope its understandable.

any ideas?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:33:54+00:00Added an answer on May 29, 2026 at 11:33 pm

    If you’re going to store the time for when you last visited the site you only need to store one time.

    So for your example:

    $current_time          = time();
    $last_visit_time       = $data->getTime($userId);
    $since_last_visit_time = $current_time - $last_visit_time;
    $six_hours_in_seconds  = 21600;
    
    if($since_last_visit_time > $six_hours_in_seconds) {
        // not sure of the function call here so using yours
        // store new time as it's been over 6 hours
        $storeTime->timestore($current_time);
        $remaining_time = $six_hours_in_seconds;
    } else {
        $remaining_time = $six_hours_in_seconds - $since_last_visit_time;
    }
    
    echo "Remaining Seconds: {$remaining_time}<br />";
    

    Part 2 using JavaScript / Ajax you can use this to display the remaning time

    Demo:

    • http://jsfiddle.net/kLk4f/1/

    JS

    var time_in_seconds = 21600; // this would be the $remaining_time PHP variable
    
    setInterval(function() {
        $('#countdown').html(seconds2time(time_in_seconds));
        time_in_seconds--;
    }, 1000);
    
    function seconds2time(seconds) {
        var hours   = Math.floor(seconds / 3600);
        var minutes = Math.floor((seconds - (hours * 3600)) / 60);
        var seconds = seconds - (hours * 3600) - (minutes * 60);
        var time = "";
    
        if (hours != 0) {
          time = hours+":";
        }
        if (minutes != 0 || time !== "") {
          minutes = (minutes < 10 && time !== "") ? "0"+minutes : String(minutes);
          time += minutes+":";
        }
        if (time === "") {
          time = seconds+"s";
        }
        else {
          time += (seconds < 10) ? "0"+seconds : String(seconds);
        }
        return time;
    }
    

    HTML

    <span id="countdown"></span>​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this situation that I need to let users define decisions based on
I am in a situation where I need to calculate Percentage for every fiscal
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to dynamically build up a list of
I have a situation where I need to pass two parameters to an action.
I have a situation where I need to update a control referenced in a
I have a situation where I need to notify some users when something in
I have a situation where I need to find the value with the key
I have a situation where i need to send commands to a running java
I have a situation where I need to concatenate several string to form an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.