处理php mktime和date时的奇怪结果

时间:2013-02-15 21:18:46

标签: php strtotime mktime

我需要根据商店的营业时间加上抵消产生交货时间。正确生成打开时间,并正确生成偏移量,但我似乎无法正确组合它们。我尝试了几种方法:

$open = array('open_hr' => 14, 'open_min' => 0);

$tmpTime = mktime($open['open_hr'], $open['open_min']);
$openTime = date('G:i', $tmpTime);

$tmpOffset = 90; //in minutes
$tmpOpenWithOffset = $tmpTime + $tmpOffset * 60;
$finalTime = date('G:1', $tmpOpenWithOffset);

echo "open time: " . $openTime . "; offset in minutes: " .$tmpOffset . "; final time: " .$finalTime;

结果是:

  开放时间:14:00;以分钟为单位的偏移:90;最后时间:15:1

我希望在14:00之后加上90分钟会导致15:30。

我也是这样试过的:

$open = array('open_hr' => 14, 'open_min' => 0);
$tmpTime = mktime($open['open_hr'], $open['open_min']);
$openTime = date('G:i', $tmpTime);

$tmpOffset = 90;
$finalTime = strtotime('+'.$tmpOffset . ' minutes', $openTime);

echo "open time: " . $openTime . "; offset in minutes: " .$tmpOffset . "; final time: " .$finalTime;

这导致:

open time: 14:00; offset in minutes: 90; final time: 5414

我也尝试使用eval来评估strtotime的第一个参数,如下所示:

$open = array('open_hr' => 14, 'open_min' => 0);
$tmpTime = mktime($open['open_hr'], $open['open_min']);
$openTime = date('G:i', $tmpTime);

$tmpOffset = 90;
$tmpStr = eval('+'.$tmpOffset . ' minutes');
$finalTime = strtotime($tmpStr, $openTime);

echo "open time: " . $openTime . "; offset in minutes: " .$tmpOffset . "; final time: " .$finalTime;

这导致:

Parse error: syntax error, unexpected $end in /t.php(7) : eval()'d code on line 1
open time: 14:00; offset in minutes: 90; final time: 

同样,期望的结果是在14:00之前添加90分钟将返回15:30。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

错字:

$finalTime = date('G:1', $tmpOpenWithOffset);

将I替换为1可以在您采取第一个解决方案时解决您的问题