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

发布于 2024-08-26 19:22:19 字数 279 浏览 9 评论 0原文

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

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

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

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

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

$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.

What is the best way to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

缘字诀 2024-09-02 19:22:19

最直接的方法是简单地“make

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

我想计算出今天还剩多少秒,然后只添加那么多秒以获得第二天第一分钟的 unix 时间戳。

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

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.

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.

夏有森光若流苏 2024-09-02 19:22:19

您可以轻松地获得明天的午夜时间戳:

$tomorrow_timestamp = strtotime('tomorrow');

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

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

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"))));
凉风有信 2024-09-02 19:22:19
$tomorrow = strtotime('+1 day', strtotime(date('Y-m-d')));
$secondsLeftToday = time() - $tomorrow;
$tomorrow = strtotime('+1 day', strtotime(date('Y-m-d')));
$secondsLeftToday = time() - $tomorrow;
温折酒 2024-09-02 19:22:19

像这样简单的东西:

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

就是我会用的。

Something simple like:

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

is what I'd use.

撧情箌佬 2024-09-02 19:22:19

第二天的开始时间是这样计算的:

<?php

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

echo date('r', $allowable_start_date);

?>

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

<?php

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

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

?>

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);

?>
GRAY°灰色天空 2024-09-02 19:22:19

我的变体:

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

My variant:

 $allowable_start_date = strtotime('today +1 day');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文