时间错误(我的脚本)PHP
我遇到了这个奇怪的错误。我基本上只是使用以下函数在格式为“12:20pm”的时间中添加几分钟...
function calc_arb_time($startTime, $amount){
$startTime = date('Y-m-d') . substr($startTime,0,-2);
$startTime = strtotime($startTime);
$seconds = $amount*60;
$startTime += $seconds;
$newStartTime = date('g:ia', $startTime);
return($newStartTime);
}
echo calc_arb_time('12:20pm',20); // <-- this returns 12:40pm which is great
echo calc_arb_time('1:20pm',20); // this returns 1:40am... Why the AM??
I've got this weird bug happening. I'm basically just adding a few minutes to a time formatted like '12:20pm' with the following function...
function calc_arb_time($startTime, $amount){
$startTime = date('Y-m-d') . substr($startTime,0,-2);
$startTime = strtotime($startTime);
$seconds = $amount*60;
$startTime += $seconds;
$newStartTime = date('g:ia', $startTime);
return($newStartTime);
}
echo calc_arb_time('12:20pm',20); // <-- this returns 12:40pm which is great
echo calc_arb_time('1:20pm',20); // this returns 1:40am... Why the AM??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有采取任何措施来保留 am 或 pm 部分吗?如果您不打算使用日期部分,也不必担心它。
这段代码更简单并且工作正常:
而且我不确定函数的名称反映了它的作用。你应该考虑改变它。
You're not doing anything to preserve the am or pm part? Also don't worry about the date part if you're not going to use it anyway.
This code is simpler and it works fine:
Also I'm not sure the name of the function reflects what it does. You should consider changing it.
您可能还想查看 DateTime 类
:
You might also want to look at the DateTime class:
Prints: