I am using jquery.countdown.js for time calculating If i change the time on local pc the counter will change I want to get the time from server and compare it with the time saved in Database. Jquery code is given below and the anonymous function called is also given below.
$('#paper_countdown').countdown({
timestamp : (new Date($('.intime_time').val())),
callback : function(days, hours, minutes, seconds){
var message = "";
message += days + " day" + ( days==1 ? '':'s' ) + ", ";
message += hours + " hour" + ( hours==1 ? '':'s' ) + ", ";
message += minutes + " minute" + ( minutes==1 ? '':'s' ) + " and ";
message += seconds + " second" + ( seconds==1 ? '':'s' ) + " <br />";
if(newYear){
message += "left until the new year!";
}
else {
message += "left to 10 days from now!";
}
note.html(message);
}
});
This is the anonymous function from jquery.countdown.js
var days = 24 * 60 * 60,
hours = 60 * 60,
minutes = 60;
// Creating the plugin
$.fn.countdown = function (prop) {
var options = $.extend({
callback: function () {
},
timestamp: 0
}, prop);
var left, d, h, m, s, positions;
// Initialize the plugin
init(this, options);
positions = this.find('.position');
(function tick() {
left = Math.floor((options.timestamp - (new Date())) / 1000);
if (left < 0) {
left = 0;
}
// Number of hours left
h = Math.floor(left / hours);
updateDuo(2, 3, h);
left -= h * hours;
// Number of minutes left
m = Math.floor(left / minutes);
updateDuo(4, 5, m);
left -= m * minutes;
// Number of seconds left
s = left;
updateDuo(6, 7, s);
// Calling an optional user supplied callback
options.callback(h, m, s);
// Scheduling another call of this function in 1s
if (h == 0 && m == 0 && s == 0)
location.reload();
else
setTimeout(tick, 1000);
})();
// This function updates two digit positions at once
function updateDuo(minor, major, value) {
switchDigit(positions.eq(minor), Math.floor(value / 10) % 10);
switchDigit(positions.eq(major), value % 10);
}
return this;
};
My Php code to calculate the remaining time is
public function remainingTime($exam_id, $sub_id,$std_reg_detail_id){
$papers = \backend\models\Papers::find()->where('exam_id =:eid AND id =:sid',[':eid'=>$exam_id, ':sid'=>$sub_id])->one();
$std_reg_detail = \backend\models\StudentRegDetail::find()->where(['paper_id'=>$sub_id,'reg_exam_id'=>$exam_id,'id'=>$std_reg_detail_id])->one();
$rem_time = 0;
if($papers != NULL){
$time = $std_reg_detail['paper_date'].' '.$std_reg_detail['paper_time'];
$total_time = $papers['total_time_allowed'];
$dt = new \DateTime($time);
$start_time = $dt->format('Y-m-d H:i:s');
$dt->add(new \DateInterval('PT' . $total_time . 'M'));
$end_time = $dt->format('d M Y H:i:s');
$rem_time = $end_time;
// dd($rem_time);
}
return $rem_time;
}
To display in the view
<div class="clock">
<input type="hidden" value="<?php echo $rem_time ?>" class="intime_time"/>
<h3>Remaining Time</h3>
<div id="paper_countdown"></div>
</div>