如何在php中找到第二天开始的unix时间戳?

zec*_*ude 18 php unix-timestamp

我有一个当前时间的unix时间戳.我想获得第二天开始的unix时间戳.

$current_timestamp = time();
$allowable_start_date = strtotime('+1 day', $current_timestamp);
Run Code Online (Sandbox Code Playgroud)

正如我现在所做的那样,我只是将整整一天添加到unix时间戳中,相反,我想知道在当前这一天剩下多少秒,并且只添加那么多秒才能获得unix第二天第一分钟的时间戳.

最好的方法是什么?

dec*_*eze 26

简单地" 制造 "那个时间的最简单的方法:

$tomorrowMidnight = mktime(0, 0, 0, date('n'), date('j') + 1);
Run Code Online (Sandbox Code Playgroud)

引用:

我想弄清楚当天剩下多少秒,并且只添加那么多秒才能获得第二天第一分钟的unix时间戳.

不要那样做.尽可能避免相对计算,特别是如果在没有秒算术的情况下"绝对"获得时间戳是如此微不足道.

  • 所以这与strtotime('+ 1天',mktime(0,0,0))相同? (2认同)
  • @zeckdude基本上是的,我会说效率更高,因为`strtotime`功能强大,但速度较慢.此外,您明确地在午夜*制作时间戳*,而不是在午夜时间戳添加一天,这在边缘情况下可能会或可能不会更加可靠,例如夏令时切换等. (2认同)

小智 8

您可以在午夜时间戳中轻松获得:

$tomorrow_timestamp = strtotime('tomorrow');
Run Code Online (Sandbox Code Playgroud)

如果您希望能够执行可变天数,则可以轻松地执行此操作:

$days = 4;
$x_num_days_timestamp = strtotime(date('m/d/Y', strtotime("+$days days"))));
Run Code Online (Sandbox Code Playgroud)