致命错误:在整数上调用成员函数format()

时间:2016-07-10 16:02:17

标签: php

    $total = $_POST['total'];
$myselect = $_POST['myselect'];

$t = '2';
// function date
$date_go = "$rn_gostart"; //กำหนดค้นวันที่เริ่ม
$date_end = "$rn_endstart"; //กำหนดค้นวันที่กลับ
$datetime1 = new DateTime($date_go);
$datetime2 = new DateTime($date_end);
$datetotal = $datetime1->diff($datetime2);

if ($datetotal = 0 ) {
$total = '1800';
}else {
 $total= $datetotal->format('%a') * 1800*$myselect;  //ผลการคำนวณ
}

问题是,如果我预订这样的一天:10.07.2559 * 07.10.2559 = 0,但是如果它会预订10/01/2559 * 11/07/2559。这是正常的

2 个答案:

答案 0 :(得分:1)

变化:

if ($datetotal = 0 )

为:

if ($datetotal == 0 )

您当前的代码为$datetotal分配零,使您的代码失败:

$total = $datetotal->format('%a') * 1800*$myselect;

格式%a不正确。它必须由a valid format替换,具体取决于您要执行的操作。

答案 1 :(得分:0)

如果我不使用if else并使用

$total = $_POST['total'];
$myselect = $_POST['myselect'];

// function date
$date_go = "$rn_gostart"; //กำหนดค้นวันที่เริ่ม
$date_end = "$rn_endstart"; //กำหนดค้นวันที่กลับ
$datetime1 = new DateTime($date_go);
$datetime2 = new DateTime($date_end);
$datetotal = $datetime1->diff($datetime2);
$total= $datetotal->format('%a') * 1800 * $myselect;

10.07.2559 * 08.10.2559 = 1800

但是10.07.2559 * 07.10.2559 = 0