我正在使用jquery倒计时器,但问题是每次页面刷新时计时器从值设置开始。我希望第一次得到时间并保存在PHP会话中并从第一次减去第二次更新会话等。在简单的Word中我想创建php倒计时器只使用php运行50
这是我的控制器代码
public function actionViewtimer($id) {
$session = Yii::app()->session;
$this->layout = 'countdownlayout';
if (empty($session['countdowntime'])) {
$orderTime = microtime(true); #when user first time enter timer start from 50 mins
$session->add('countdowntime', array($orderTime)); //replace this line
} else {
$time_end = microtime(true);
$orderTime = $time_end - $time_start;
unset($session['countdowntime']);//unset old session
$session->add('countdowntime', array($orderTime)); //when user enter second
}
$getMinutes = $getMinutes_from_orderTime ; #please tell me how we extact minutes and pass them to view
$session->add('timersession', array($rand));
$this->render('viewtimerpage', array(
'getMinutes' => $getMinutes
));
}
这是我的jquery代码
<script>
$(function() {
$('#xlarge').countdowntimer({
minutes: 50<?php echo $getMinutes; ?>,
size: "lg",
timeUp: timeisUp
});
});
function timeisUp() {
alert('Time Complete');
return false;
}
</script>
答案 0 :(得分:0)
抱歉,我没有使用Yii,所以这个例子将使用普通的PHP。
session_start();
if(!isset($_POST['counter']) || !is_array($_POST['counter')) {
$duration = 50;
$counter = array(
'start_time' => new DateTime()->format('Y-m-d H:i:s'),
'end_time' => new DateTime()->modify("+{$duration} minute")->format('Y-m-d H:i:s'),
'duration' => $duration
);
$_SESSION['counter'] = $counter;
}
$counter = $_POST['counter'];
$now = new DateTime();
$end_time = new DateTime($counter['end_time']);
$time_left = $end_time->getTimestamp() - $now->getTimestamp();
echo "{$time_left} seconds left";
这应该足够简单转换,您可以轻松地将秒转换为分钟。
答案 1 :(得分:0)
不要销毁会话变量。只需在第一次设置为空时将其设置为倒数计时器。
在你的行动中
$session = Yii::app()->session;
$this->layout = 'countdownlayout';
if (empty($session['countdowntime'])) {
$date = new DateTime();
$date->modify('+50 minutes'); // Add 50 minutes to current time
$session['countdowntime'] = $date->format('Y/m/d H:i:s'); // Store date in session
}
$this->render('viewtimerpage', array(
'countdowntime' => $session['countdowntime'];
));
jQuery代码
$(function() {
$('#xlarge').countdowntimer({
dateAndTime : "<?php echo $countdowntime; ?>",
size: "lg",
timeUp: timeisUp
});
});
答案 2 :(得分:0)
据我了解,您希望显示两个日期之间的差异。默认值= 50分钟(如果用户第一次打开页面,会话值)。如果用户在视图中10分钟后刷新页面必须显示差异(40)。它会是这样的:
public function actionViewtimer($id) {
$this->layout = 'countdownlayout';
/** @var CHttpSession $session */
$session = Yii::app()->session;
$minutes = 50; //default value for minutes
if (!$session->offsetExists('countdowntime')) {
$session->add('countdowntime', new DateTime()); //set value to session
} else {
$timeEnd = new DateTime();
/** @var DateTime $countDownTime */
$countDownTime = $session->get('countdowntime');
$interval = $countDownTime->diff($timeEnd);
$countMinutes = $interval->days * 24 * 60;
$countMinutes += $interval->h * 60;
$countMinutes += $interval->i;
$minutes -= $countMinutes;
$session->add('countdowntime', $timeEnd); //refresh value
}
// $session->add('timersession', array($rand)); don't know for what is it
$this->render('viewtimerpage', array(
'getMinutes' => $minutes
));
}
和js:
<script>
$(function() {
$('#xlarge').countdowntimer({
minutes: <?php echo $getMinutes; ?>, //without 50