这次如何计算(php session)

时间:2017-01-18 14:51:45

标签: php datetime session

session_start();

$_SESSION['start1']= new DateTime('now');

$_SESSION['start2']= new DateTime('now +10 minute');

$_SESSION['time1'] = $_SESSION['start2']->diff($_SESSION['start1']);

$_SESSION['start3']= new DateTime('now +20 minute');

$_SESSION['start4']= new DateTime('now +30 minute');

$_SESSION['time2'] = $_SESSION['start3']->diff($_SESSION['start4']);

$_SESSION['alltime']=$_SESSION['time1']+$_SESSION['time1'];

echo $_SESSION['alltime']->format('%I minute %S second');

但是没有显示时间?

1 个答案:

答案 0 :(得分:0)

您需要启用错误报告:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

这会告诉你问题。

您的代码:

session_start();

$_SESSION['start1']= new DateTime('now');

$_SESSION['start2']= new DateTime('now +10 minute');

$_SESSION['time1'] = $_SESSION['start2']->diff($_SESSION['start1']);

$_SESSION['start3']= new DateTime('now +20 minute');

$_SESSION['start4']= new DateTime('now +30 minute');

$_SESSION['time2'] = $_SESSION['start3']->diff($_SESSION['start4']);

$_SESSION['alltime']=$_SESSION['time1']+$_SESSION['time1']; <----- $_SESSION[ 'alltime' ] is an integer

echo $_SESSION['alltime']->format('%I minute %S second');

现在很可能有更好的方法来完成这个,但我已经试了一下。

$start1 = new DateTime('now');
$start2= new DateTime('now +10 minute');

$time1 = $start2->diff($start1);
$time1_min = $time1->format( '%I' );
$time1_sec = $time1->format( '%S' );
echo $time1->format( '%I minutes %S seconds' )."\n";
echo $time1_min."\n";

$start3= new DateTime('now +20 minute');
$start4= new DateTime('now +30 minute');

$time2 = $start3->diff($start4);
$time2_min = $time2->format( '%I' );
$time2_sec = $time2->format( '%S' );
echo $time2_min."\n";
echo $time2->format( '%I minutes %S seconds' )."\n";

$total_min = $time1_min + $time2_min;
$total_sec = $time1_sec + $time2_sec;

echo "Total is $total_min minutes and $total_sec seconds";

基本上它是找到分钟和秒的差异并将它们加在一起,现在你必须明白,如果秒数超过60,那么它将不会添加到分钟,所以你需要解决这个问题。

还必须删除会话,因为eval.in并不喜欢它。 https://eval.in/719160