【问题标题】:Php subtract time saved in session with current timephp减去当前时间保存在会话中的时间
【发布时间】:2015-06-03 03:56:04
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 我不确定您的问题是什么。你能更好地解释什么应该开始,什么应该停止计时器吗?
  • 我不确定您真正尝试做什么,正如@HenriqueBarcelos 所说。但是闪存数据可能会对您有所帮助:yiiframework.com/doc-2.0/…
  • 是的,我正在使用 HenriqueBarcelos,当我登陆计时器页面时,计时器从 50 秒开始,假设我在 15 秒后关闭浏览器并再次打开计时器页面,它从 35 秒开始 50-15= 35
  • 我认为他正在尝试拥有一个持久的倒计时。

标签: php session datetime yii


【解决方案1】:

对不起,我没有用过 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";

这应该很容易转换,您可以轻松地将秒转换为分钟。

【讨论】:

    【解决方案2】:

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

    内在行动

    $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
        });
    });
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 嗨,Danila Ganchar 代码不起作用,我设置了 getMinutes=5,当我在 1 分钟后第一次刷新页面时它在 getMinutes 中显示 4,当我第二次刷新时它仍然显示 4,如果再次刷新它在 getMINutes 中显示 5
      • 1 分钟后必须是哪个值? 4 对吗? 2 分钟后,用户更新了页面。必须是哪个值? 5-1-2=3 对吗?
      • if (!$session->offsetExists('countdowntime')) { $timeFirst = strtotime(date("Y-m-d h:i:s", time())); $session->add('countdowntime', array($timeFirst)); //设置会话值 } else { $timeFirst = $session['countdowntime'][0]; $timeSecond = strtotime(date("Y-m-d h:i:s", time())); $differenceInSeconds = $timeSecond - $timeFirst; $differenceInSeconds = $differenceInSeconds / 60; $getminutes = round($differenceInSeconds, 0); $分钟 = $分钟 - $getminutes; }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2020-06-02
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多