Php用当前时间减去会话中节省的时间

Php用当前时间减去会话中节省的时间,php,session,datetime,yii,Php,Session,Datetime,Yii,我使用的是jquery倒计时计时器,但问题是每次页面刷新时计时器都从设置的值开始。我想第一次得到时间并保存在PHP会话中,从第一次和更新会话中减去第二次。简单地说,我想用PHP创建只运行50次的PHP倒计时 这是我的控制器代码 public function actionViewtimer($id) { $session = Yii::app()->session; $this->layout = 'countdownlayout'; if (empty($s

我使用的是jquery倒计时计时器,但问题是每次页面刷新时计时器都从设置的值开始。我想第一次得到时间并保存在PHP会话中,从第一次和更新会话中减去第二次。简单地说,我想用PHP创建只运行50次的PHP倒计时

这是我的控制器代码

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>

$(函数(){
$('#xlarge')。倒计时({
分钟:50,
尺寸:“lg”,
timeUp:timeisUp
});
});
函数timeisUp(){
警报(“时间完成”);
返回false;
}

很抱歉,我没有使用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";

这应该足够简单,可以进行转换,并且您可以轻松地将秒转换为分钟。

不要破坏会话变量。只需在它第一次为空时设置它,然后将该值始终插入倒计时

你内心的行动

$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
    });
});
$(函数(){
$('#xlarge')。倒计时({
日期和时间:“”,
尺寸:“lg”,
timeUp:timeisUp
});
});

据我所知,您希望显示两个日期之间的差异。默认值=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

$(函数(){
$('#xlarge')。倒计时({
分钟:,//不含50分钟

我不确定你的问题是什么。你能更好地解释一下计时器应该启动什么和应该停止什么吗?我不确定你到底想做什么,正如@HenriqueBarcelos所说。但是flash数据可能会帮助你:是的,我正在使用HenriqueBarcelos,当我进入计时器页面时,计时器从50秒开始,假设我关闭浏览器并打开n计时器页面15秒后再次刷新,它从35秒开始50-15=35我想他正在尝试使用一个持久的倒计时计时器。嗨,Danila Ganchar代码不工作,我设置了getMinutes=5,当我在1分钟后第一次刷新页面时,它在getMinutes中显示4,当我第二次刷新时,它仍然显示4,如果再次刷新,它在中显示5GetMinutes哪一个值必须在1分钟后?4是正确的吗?2分钟后,用户已更新页面。哪一个值必须是?5-1-2=3是正确的吗?如果(!$session->offsetExists('countdowntime')){$timeFirst=strottime(date(“Y-m-d h:i:s”,time());$session->add('countdowntime',array($timeFirst));//将值设置为session}else{$timeFirst=$session['countdown'][0];$timeSecond=strottime(日期(“Y-m-d h:i:s”,time());$differenceInSeconds=$timeSecond-$timeFirst;$differenceInSeconds=$differenceInSeconds/60;$getminutes=round($differenceInSeconds,0);$minutes=$minutes-$getminutes;}