php 在午夜获取今天的时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13129817/
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
Getting a timestamp for today at midnight?
提问by user1701398
How would I go about getting a timestamp in php for today at midnight. Say it's monday 5PM and I want the Timestamp for Monday(today) at midnight(12 am) which already has happened.
我将如何在 php 中获取今天午夜的时间戳。假设现在是星期一下午 5 点,我想要星期一(今天)午夜(上午 12 点)的时间戳,这已经发生了。
Thank you
谢谢
回答by hakre
$timestamp = strtotime('today midnight');
You might want to take a look what PHP has to offer: http://php.net/datetime
您可能想看看 PHP 提供了什么:http: //php.net/datetime
回答by Joe Meyer
I think that you should use the new PHP DateTime object as it has no issues doing dates beyond the 32 bit restrictions that strtotime() has. Here's an example of how you would get today's date at midnight.
我认为您应该使用新的 PHP DateTime 对象,因为它在处理超出 strtotime() 的 32 位限制的日期时没有问题。下面是一个示例,说明如何在午夜获取今天的日期。
$today = new DateTime();
$today->setTime(0,0);
Or if you're using PHP 5.4 or later you can do it this way:
或者,如果您使用的是 PHP 5.4 或更高版本,您可以这样做:
$today = (new DateTime())->setTime(0,0);
Then you can use the echo $today->format('Y-m-d');to get the format you want your object output as.
然后您可以使用echo $today->format('Y-m-d');来获取您希望对象输出的格式。
回答by bearcat
Today at midnight. Easy.
今天午夜。简单。
$stamp = mktime(0, 0, 0);
回答by William Entriken
You are looking to calculate the time of the most recent celestial event where the sun has passed directly below your feet, adjusted for local conventions of marking high noon and also potentially adjusting so that people have enough daylight left after returning home from work, and for other political considerations.
您正在计算最近一次天体事件的时间,即太阳直接从您脚下经过的时间,根据当地的正午惯例进行调整,并可能进行调整,以便人们下班回家后有足够的日光,以及其他考虑。
Daunting right? Actually this is a common problem but the complete answer is location-dependent:
令人生畏吗?实际上,这是一个常见问题,但完整的答案取决于位置:
$zone = new \DateTimeZone('America/New_York'); // Or your own definition of “here”
$todayStart = new \DateTime('today midnight', $zone);
$timestamp = $todayStart->getTimestamp();
Potential definitions of “here” are listed at https://secure.php.net/manual/en/timezones.php
回答by SamHuckaby
$today_at_midnight = strtotime(date("Ymd"));
should give you what you're after.
应该给你你想要的。
explanation
解释
What I did was use PHP's date function to get today's date without any references to time, and then pass it to the 'string to time' function which converts a date and time to a epoch timestamp. If it doesn't get a time, it assumes the first second of that day.
我所做的是使用 PHP 的 date 函数获取今天的日期而不引用任何时间,然后将其传递给“string to time”函数,该函数将日期和时间转换为纪元时间戳。如果没有时间,它会假设当天的第一秒。
References: Date Function: http://php.net/manual/en/function.date.php
参考资料:日期函数:http: //php.net/manual/en/function.date.php
String To Time: http://us2.php.net/manual/en/function.strtotime.php
回答by Jan Galtowski
In more object way:
以更客观的方式:
$today = new \DateTimeImmutable('today');
example:
例子:
echo (new \DateTimeImmutable('today'))->format('Y-m-d H:i:s');
// will output: 2019-05-16 00:00:00
and:
和:
echo (new \DateTimeImmutable())->format('Y-m-d H:i:s');
echo (new \DateTimeImmutable('now'))->format('Y-m-d H:i:s');
// will output: 2019-05-16 14:00:35
回答by Case
$timestamp = strtotime('today midnight');
is the same as
是相同的
$timestamp = strtotime('today');
and it's a little less work on your server.
它在您的服务器上的工作少了一点。
回答by Victor Mbamara
$midnight = strtotime('midnight');is valid
You can also try out
strtotime('12am')or strtotime('[input any time you wish to here. e.g noon, 6pm, 3pm, 8pm, etc]'). I skipped adding today before midnight because the default is today.
$midnight = strtotime('midnight');是有效的
你也可以试试
strtotime('12am')或strtotime('[input any time you wish to here. e.g noon, 6pm, 3pm, 8pm, etc]')。我今天在午夜之前跳过了添加,因为默认值是今天。
回答by nowox
If you are using Carbon you can do the following. You could also format this date to set an ExpireHTTP Header.
如果您使用的是 Carbon,您可以执行以下操作。您还可以格式化此日期以设置ExpireHTTP 标头。
Carbon::parse('tomorrow midnight')->format(Carbon::RFC7231_FORMAT)
回答by aspile
Updated Answer in 19 April, 2020
2020 年 4 月 19 日更新的答案
Simply we can do this:
我们可以这样做:
$today = date('Y-m-d 00:00:00');
$today = date('Y-m-d 00:00:00');

