Patrioticcow
Patrioticcow

Reputation: 27038

how to calculate the remaining left time with php and mysql?

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

Upvotes: 1

Views: 1847

Answers (2)

Phill Pafford
Phill Pafford

Reputation: 85298

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:

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>​

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31627

Use TIME_TO_SEC(TIMEDIFF(final_time, initial_time))

SELECT TIME_TO_SEC(TIMEDIFF('17:00:00', '09:00:00')) -- 28800

SELECT TIME_TO_SEC(TIMEDIFF('12:30:00', '12:00:00')) -- 1800
SELECT TIME_TO_SEC(TIMEDIFF('10:30:00', '10:15:00')) -- 900

Upvotes: 2

Related Questions