0

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

$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

我预计将 90 分钟添加到 14:00 将导致 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。我在这里做错了什么?

4

1 回答 1

0

错字:

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

当您采用第一个解决方案时,将 I 替换为 1 应该可以解决您的问题

于 2013-02-15T21:29:19.630 回答