【发布时间】:2019-12-05 20:35:15
【问题描述】:
我想得到明天中午 12 点的时间戳,我可以使用下面的代码行来做到这一点
strtotime('tomorrow noon');
但我遇到的问题是,当当前时间是午夜 00:00 时,我正在获取第二天的时间戳,我想要这样的时间是午夜 00:00 时,它仍然应该给出时间戳新一天的中午而不是第二天。
【问题讨论】:
我想得到明天中午 12 点的时间戳,我可以使用下面的代码行来做到这一点
strtotime('tomorrow noon');
但我遇到的问题是,当当前时间是午夜 00:00 时,我正在获取第二天的时间戳,我想要这样的时间是午夜 00:00 时,它仍然应该给出时间戳新一天的中午而不是第二天。
【问题讨论】:
你可以做一个简单的三元:
strtotime((date('H:i') == '00:00' ? '' : 'tomorrow ').'noon');
或者用一个简单的 if 情况:
if (date('H:i') == '00:00') {
$noon = strtotime('noon');
} else {
$noon = strtotime('tomorrow noon');
}
【讨论】:
我想如果还没有中午,您可能希望在中午显示当前日期,否则显示明天中午的时间。如果是这种情况,以下内容可能会有所帮助:
$noon = strtotime('now') < strtotime('noon')
? strtotime('noon')
: strtotime('tomorrow noon')
【讨论】: