Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中编写这个if-conditional?_Php - Fatal编程技术网

如何在PHP中编写这个if-conditional?

如何在PHP中编写这个if-conditional?,php,Php,我的变量,$current和$row['start']的格式为2012-07-24 18:00:00 我应该如何写以下内容 if($row['start']-$current

我的变量,
$current
$row['start']
的格式为2012-07-24 18:00:00

我应该如何写以下内容

if($row['start']-$current<2小时)echo“很快开始”

此外,是否有一种方法将其与以下内容结合起来

<?php echo ($current > $row['start']) ? 'Started' : 'Starts';  ?>

您可以使用将这些日期时间字符串转换为时间戳,然后可以相互添加和减去时间戳

$diff = strtotime($row['start']) - strtotime($current);
if ($diff < 7200) {
    echo 'Starts soon';
} else if ($diff <= 0) {
    echo 'Started';
} else {
    echo 'Starts';
}
$diff=strottime($row['start'])-strottime($current);
如果($diff<7200){
回声“很快开始”;

}否则,如果($diff我建议以秒为单位工作(从epoch开始),那么strotime()适合:

define("SOON_THRESHOLD", 2*60*60); // 7200 seconds == 2 hours
$start_time = strtotime($row['start']);
$current_time = strtotime($current);
$seconds_til_start = $start_time - $current_time;

if($seconds_til_start < SOON_THRESHOLD) {
  ...
}
define(“SOON_THRESHOLD”,2*60*60);//7200秒==2小时
$start_time=strottime($row['start']);
$current\u time=strottime($current);
$seconds_til_start=$start_time-$current_time;
如果($s\u直到\u开始<很快\u阈值){
...
}

谢谢。我想如果不将它们转换为时间戳,就无法做到这一点。不,您可以使用