我想比较两次。 但首先我想数一次110分钟。 我做错了什么?
代码:
$current_time = date("H:i");
$match_start = strtotime("H:i", "19:30"); // <- Value from database
$match_end = strtotime("+110 minutes", $match_start)
if($current_time > $match_start && $current_time < $match_end) {
//Match has started
}
答案 0 :(得分:1)
这可能与您正在比较字符串而不是实际时间值这一事实有关。尝试使用DateTime()
,这样可以更清楚。
$current_time = new DateTime();
$match_start = new DateTime("19:30");
$match_end = (new DateTime("19:30"))->modify("+110 minutes");
if($current_time > $match_start && $current_time < $match_end) {
//Match has started
}
答案 1 :(得分:1)
使用strtotime()
$current_time = strtotime(date("H:i")); // or strtotime(now);
$match_start = strtotime("14:30");
$match_end = strtotime("+110 minutes", $match_start);
if($current_time > $match_start && $current_time < $match_end) {
echo "Match has started";
}
答案 2 :(得分:0)
首先,像这样创建日期时间(更改日期时区):
$match_start = "19:30"; // <- Value from database
$current_time = new DateTime('', new DateTimeZone('Europe/Rome'));
$start = new DateTime($match_start, new DateTimeZone('Europe/Rome'));
$end = new DateTime($match_start, new DateTimeZone('Europe/Rome'));
然后将110分钟添加到结束时间:
$end->add(new DateInterval('PT110M'));
最后:
if($current_time > $start && $current_time < $end)
{
//Match has started
}