php - 获取 strtotime 以查找给定时间的下一个时间戳

标签 php timestamp strtotime

我试图让 strtotime 给我一个时间戳,但我只知道它是一小时一分钟,而且是该行中的下一个。外汇。

如果给定的小时和分钟是 01:00 (AM),我要求它返回 01:00 (AM) 的时间戳,如果是前一天 19:00,我希望它返回时间戳第二天的 01:00,但如果是午夜之后的 00:30,则应返回同一日期的时间戳 01:00。

为了简化它,它是下一次出现的时间。

Strtotime 给我的是同一天的时间,如果不是下一次发生的时间。

最佳答案

您可以轻松生成时间戳,查看它是否已经过去,然后增加日期。

也许是这样的?

$timestamp = strtotime("1:00 am");
if($timestamp < time()) {
    $timestamp = strtotime("+1 day",$timestamp);
}

或者,如果您想将其收紧为单行:

$string = "1:00 am";
$timestamp = (strtotime($string) > time()) ? strtotime($string) : strtotime("tomorrow " . $string);

关于php - 获取 strtotime 以查找给定时间的下一个时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227664/

相关文章:

php - 向 A* php 实现添加非单调启发式

php - fatal error : Uncaught PDOException: SQLSTATE[42000]:?

php - 我想在我的网站上添加一些检测;这些是好的选择吗?

php - 在 PHP 中确定客户端操作系统

r - 如何将时间戳字符串 "2014-07-20T05:11:49.988Z"转换为 R 中的 POSIXct?

php - 获取特定提供日期的午夜时间戳

PHP 将整数转换为日期,strtotime 的反转

python - 跟踪 Dataframe 中的操作

mysql - 时间戳默认值与 current_timestamp 不同

PHP strtotime() 本周周一的不同结果