PHP当前时间(小时):分钟
我想有两个变量,当前时间以小时为单位:分钟和+15分钟。但首先我还必须加上+6小时 例如,现在是PHP当前时间(小时):分钟,php,date,datetime,timestamp,Php,Date,Datetime,Timestamp,我想有两个变量,当前时间以小时为单位:分钟和+15分钟。但首先我还必须加上+6小时 例如,现在是2018-01-07 16:35:10。首先我加上+6小时。所以它将是2018-01-07 22:35:10。接下来,我只想提取小时:分钟 我只想得到“22:35”的变量 下一个变量是22:35+15分钟,所以是22:50 所以我有$dateNow=22:35和$dateThen=22:50 到目前为止,我已经尝试过获取当前时间和+6小时,但它不起作用。错误:对整数调用成员函数format() $no
2018-01-07 16:35:10
。首先我加上+6小时。所以它将是2018-01-07 22:35:10
。接下来,我只想提取小时:分钟
我只想得到“22:35”的变量
下一个变量是22:35+15分钟,所以是22:50
所以我有$dateNow=22:35
和$dateThen=22:50
到目前为止,我已经尝试过获取当前时间和+6小时,但它不起作用。错误:对整数调用成员函数format()
$now = strtotime(date("Y-m-d H:i:s")." +6 hours");
$then = $now->format('H:i');
echo $then;
我认为在这种情况下,使用PHP中的DateTime类将非常有用。代码的问题是返回int而不是DateTime对象 我已修改了您的代码,因此它可以正常工作:
$org = new DateTime("2018-01-07 16:35:10");
$then = $org->add(new DateInterval("PT6H"));
echo $then->format("H:i"),"<br>";
$afterThen = $then->add(new DateInterval("PT15M"));
echo $afterThen->format("H:i");
$org=新日期时间(“2018-01-07 16:35:10”);
$then=$org->add(新日期间隔(“PT6H”);
echo$then->format(“H:i”),“
”;
$afterThen=$then->add(新日期间隔(“PT15M”);
echo$afterhen->format(“H:i”);
我认为在这种情况下,使用PHP中的DateTime类将非常有用。代码的问题是返回int而不是DateTime对象
我已修改了您的代码,因此它可以正常工作:
$org = new DateTime("2018-01-07 16:35:10");
$then = $org->add(new DateInterval("PT6H"));
echo $then->format("H:i"),"<br>";
$afterThen = $then->add(new DateInterval("PT15M"));
echo $afterThen->format("H:i");
$org=新日期时间(“2018-01-07 16:35:10”);
$then=$org->add(新日期间隔(“PT6H”);
echo$then->format(“H:i”),“
”;
$afterThen=$then->add(新日期间隔(“PT15M”);
echo$afterhen->format(“H:i”);
带有DateTime
和DateInterval
对象的简短解决方案:
$now = new DateTime();
$result = $now->add(new DateInterval('PT6H15M'))->format('H:i');
具有
DateTime
和DateInterval
对象的简短解决方案:
$now = new DateTime();
$result = $now->add(new DateInterval('PT6H15M'))->format('H:i');
试试这个:
$time = time() + 3600 * 6; // add 6 hours
$date = date("H:i", $time); // format date
$date_plus_15 = date("H:i", $time + 60 * 15); // format date and add 15 minutes
echo "Time: {$date} <br>";
echo "Time + 15 mins: {$date_plus_15}";
$time=time()+3600*6;//加6小时
$date=日期(“H:i”,$time);//格式日期
$date_plus_15=日期(“H:i”,$time+60*15);//格式化日期并添加15分钟
回显“时间:{$date}
”;
echo“时间+15分钟:{$date_加上_15}”;
这里的示例:试试这个:
$time = time() + 3600 * 6; // add 6 hours
$date = date("H:i", $time); // format date
$date_plus_15 = date("H:i", $time + 60 * 15); // format date and add 15 minutes
echo "Time: {$date} <br>";
echo "Time + 15 mins: {$date_plus_15}";
$time=time()+3600*6;//加6小时
$date=日期(“H:i”,$time);//格式日期
$date_plus_15=日期(“H:i”,$time+60*15);//格式化日期并添加15分钟
回显“时间:{$date}
”;
echo“时间+15分钟:{$date_加上_15}”;
这里的示例:可恢复致命错误:DateTime类的对象无法转换为字符串可恢复致命错误:DateTime类的对象无法转换为字符串iThanks<代码>$a=新日期时间(“+360分钟”)$now=$a->format('H:i');“现在就给我10美元。”
'$b=新日期时间(“+375分钟”)$然后=$b->format('H:i');那么,代码>谢谢<代码>$a=新日期时间(“+360分钟”)$now=$a->format('H:i');“现在就给我10美元。”
'$b=新日期时间(“+375分钟”)$然后=$b->format('H:i');那么,代码>