如何使用日期时间差异:PHP
How to use DateTime diff : PHP
我在函数中使用 DateTime diff 函数,为此我需要计算一组日期之间的秒数。我有这个功能:
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$can_bet = true;
$bettilltime = new DateTime(date("H:i:s", strtotime($bettilltime)));
$bettilldate = new DateTime(date("Y-m-d", strtotime($bettilldate)));
$betsettime = new DateTime(date("H:i:s", strtotime("H:i:s", $betsettime)));
$betsetdate = new DateTime(date("Y-m-d", strtotime("Y-m-d", $betsetdate)));
$timeDiff = $betsettime->diff($bettilltime);
return print $timeDiff->s;
$dateDiff = $betsetdate->diff($bettilldate);
return print $dateDiff->s;
if ($this->GetUserBalance() > $amount) {
if ($timeDiff->s >= 0) {
if ($dateDiff->s >= 0) {
$can_bet = true;
}
else {
$can_bet = false;
}
}
else {
$can_bet = false;
}
}
else {
$can_bet = false;
}
return $can_bet = false;
}
我正在 return 打印 $....Diff 以检查它们是否是一个值,但是这些总是 return 0。我尝试使用 ->d | ->m |->y | ->i | ->s | ->h | ->days
(我知道这些值不会 return 秒,我用它们来测试)以便从这些值中获取要打印的值,但是,它不显示 0 以外的值,我是什么这里做错了吗?
注
我在这里将最终的 return 设置为 false 以允许我能够停止使用它的功能,我想将我的值保留在原处。
只需进行简单的 DateTime 对象比较,这应该可行(并且还消除了很多虚假的 else
检查。
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$can_bet = false;
$bettilltime = new DateTime($bettilltime);
$bettilldate = new DateTime($bettilldate);
$betsettime = new DateTime($betsettime);
$betsetdate = new DateTime($betsetdate);
if ($this->GetUserBalance() > $amount) {
if ($betsettime <= $bettilltime) {
if ($betsetdate <= $bettilldate) {
$can_bet = true;
}
}
}
return $can_bet;
}
但是
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$can_bet = false;
$bettilltime = new DateTime($bettilldate.' '.$bettilltime);
$betsettime = new DateTime($betsetdate.' '.$betsettime);
if ($this->GetUserBalance() > $amount) {
$can_bet = $betsettime <= $bettilltime;
}
return $can_bet;
}
将 return 没有日期和时间的无意义分割的完全相同的结果
编辑
更简单:
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$bettilltime = new DateTime($bettilldate.' '.$bettilltime);
$betsettime = new DateTime($betsetdate.' '.$betsettime);
if ($this->GetUserBalance() > $amount) {
return $betsettime <= $bettilltime;
}
return false;
}
我在函数中使用 DateTime diff 函数,为此我需要计算一组日期之间的秒数。我有这个功能:
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$can_bet = true;
$bettilltime = new DateTime(date("H:i:s", strtotime($bettilltime)));
$bettilldate = new DateTime(date("Y-m-d", strtotime($bettilldate)));
$betsettime = new DateTime(date("H:i:s", strtotime("H:i:s", $betsettime)));
$betsetdate = new DateTime(date("Y-m-d", strtotime("Y-m-d", $betsetdate)));
$timeDiff = $betsettime->diff($bettilltime);
return print $timeDiff->s;
$dateDiff = $betsetdate->diff($bettilldate);
return print $dateDiff->s;
if ($this->GetUserBalance() > $amount) {
if ($timeDiff->s >= 0) {
if ($dateDiff->s >= 0) {
$can_bet = true;
}
else {
$can_bet = false;
}
}
else {
$can_bet = false;
}
}
else {
$can_bet = false;
}
return $can_bet = false;
}
我正在 return 打印 $....Diff 以检查它们是否是一个值,但是这些总是 return 0。我尝试使用 ->d | ->m |->y | ->i | ->s | ->h | ->days
(我知道这些值不会 return 秒,我用它们来测试)以便从这些值中获取要打印的值,但是,它不显示 0 以外的值,我是什么这里做错了吗?
注
我在这里将最终的 return 设置为 false 以允许我能够停止使用它的功能,我想将我的值保留在原处。
只需进行简单的 DateTime 对象比较,这应该可行(并且还消除了很多虚假的 else
检查。
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$can_bet = false;
$bettilltime = new DateTime($bettilltime);
$bettilldate = new DateTime($bettilldate);
$betsettime = new DateTime($betsettime);
$betsetdate = new DateTime($betsetdate);
if ($this->GetUserBalance() > $amount) {
if ($betsettime <= $bettilltime) {
if ($betsetdate <= $bettilldate) {
$can_bet = true;
}
}
}
return $can_bet;
}
但是
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$can_bet = false;
$bettilltime = new DateTime($bettilldate.' '.$bettilltime);
$betsettime = new DateTime($betsetdate.' '.$betsettime);
if ($this->GetUserBalance() > $amount) {
$can_bet = $betsettime <= $bettilltime;
}
return $can_bet;
}
将 return 没有日期和时间的无意义分割的完全相同的结果
编辑
更简单:
public function CanBet($bettilltime, $bettilldate, $betsettime, $betsetdate, $amount) {
$bettilltime = new DateTime($bettilldate.' '.$bettilltime);
$betsettime = new DateTime($betsetdate.' '.$betsettime);
if ($this->GetUserBalance() > $amount) {
return $betsettime <= $bettilltime;
}
return false;
}