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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2520404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:47:57  来源:igfitidea点击:

How do I find the unix timestamp for the start of the next day in php?

phpunix-timestamp

提问by zeckdude

I have a unix timestamp for the current time. I want to get the unix timestamp for the start of the next day.

我有当前时间的 unix 时间戳。我想获取第二天开始的 Unix 时间戳。

$current_timestamp = time();
$allowable_start_date = strtotime('+1 day', $current_timestamp);

As I am doing it now, I am simply adding 1 whole entire day to the unix timestamp, when instead I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.

正如我现在所做的那样,我只是在 unix 时间戳中添加一整天的时间,而我想弄清楚这一天还剩多少秒,并且只添加那么多秒以获得 unix第二天的第一分钟的时间戳。

What is the best way to go about this?

解决这个问题的最佳方法是什么?

回答by deceze

The most straightforward way to simply "make" that time:

那个时候简单地“制作”的最直接的方法:

$tomorrowMidnight = mktime(0, 0, 0, date('n'), date('j') + 1);

Quote:

引用:

I would like to figure out how many seconds are left in this current day, and only add that many seconds in order to get the unix timestamp for the very first minute of the next day.

我想弄清楚这一天还剩多少秒,并且只添加那么多秒以获得第二天第一分钟的 unix 时间戳。

Don't do it like that. Avoid relative calculations whenever possible, especially if it's so trivial to "absolutely" get the timestamp without seconds arithmetics.

不要那样做。尽可能避免相对计算,特别是如果“绝对”获得没有秒算术的时间戳是如此微不足道。

回答by tony4d

You can easily get tomorrow at midnight timestamp with:

您可以通过以下方式轻松获得明天的午夜时间戳:

$tomorrow_timestamp = strtotime('tomorrow');

If you want to be able to do a variable amount of days you could easily do it like so:

如果您希望能够执行可变天数,您可以像这样轻松地做到这一点:

$days = 4;
$x_num_days_timestamp = strtotime(date('m/d/Y', strtotime("+$days days"))));

回答by Amy B

$tomorrow = strtotime('+1 day', strtotime(date('Y-m-d')));
$secondsLeftToday = time() - $tomorrow;

回答by Mike Anchor

Something simple like:

一些简单的东西,比如:

$nextday = $current_timestamp + 86400 - ($current_timestamp % 86400);

is what I'd use.

是我会用的。

回答by álvaro González

The start of the next day is calculated like this:

第二天的开始计算如下:

<?php

$current_timestamp = time();
$allowable_start_date = strtotime('tomorrow', $current_timestamp);

echo date('r', $allowable_start_date);

?>

If it needs to follow your peculiar requirement:

如果它需要遵循您的特殊要求:

<?php

$current_timestamp = time();
$seconds_to_add = strtotime('tomorrow', $current_timestamp) - $current_timestamp;

echo date('r', $current_timestamp + $seconds_to_add);

?>

回答by pliashkou

My variant:

我的变种:

 $allowable_start_date = strtotime('today +1 day');