php - 如何在php中设置修复时间变量?
问题描述
我想在 php 中为我的 if 和 else 条件设置一个固定时间变量。例如:
$startTime = '08:00:00';
$endTime = '16:00:00';
$totalhrs = $endTime - $startTime;
echo $totalhrs;
有人知道如何在 PHP 中声明时间吗?谢谢您的帮助
解决方案
您可以尝试以下功能来检查时间戳。如果您不传递第二个参数,它将评估第一次是否已超过当前时间,否则它将比较第一次与第二次。
Function timeHasPassed($Time, $Time2 = 0) {
If ($Time2 != 0) {
$Now = new DateTime($Time2);
} Else {
$Now = new DateTime();
}
$Then = new DateTime($Time);
If ($Now > $Then) {
Return TRUE;
} Else {
Return FALSE;
/*
You can also use the below code to print out how long is left until the timestamp has passed. Keep in mind, this will return TRUE if tested as a boolean so maybe consider returning a different datatype instead of TRUE if you decide to go this route.
$Time = new DateTime($Time);
$Now = new DateTime();
$Remainder = $Time->diff($Now);
$Remainder = $Remainder->format("%h hours, %i minutes, and %s seconds!");
return $Remainder;
*/
}
}
推荐阅读
- graphql - GraphQL 模式拼接选择集中是否禁止使用列表类型的字段?
- verilog - 在没有名称的verilog中向上引用?
- webview - 反应本机webview“找不到变量”
- python - Telegram bot api如何安排通知?
- python-3.x - Python 3.8:迭代第一个和第二个元素,然后是第二个和第三个,依此类推
- laravel - Laravel Livewire 日期转换和验证
- sql-server - pyodbc - 'utf-8' 编解码器无法解码位置 n 中的字节 0xa0:无效的起始字节
- python-3.x - stereoRectify 与 stereoRectifyUncalibrated
- node.js - NodeJs:如何从套接字发射返回响应?
- azure-sql-database - 可以使用 ADF 自定义接收器排序先加载父记录然后再加载子记录吗?