如何在 PHP 中获取今天的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4766978/
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 14:18:09  来源:igfitidea点击:

How can I get today's timestamp in PHP

phpdatetimetimestamp

提问by Jiew Meng

I tried

我试过

$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));

but when I output it

但是当我输出它时

die($dtToday->format('d M Y g:i:s a'));

I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?

我仍然得到时间,例如“2011 年 1 月 22 日下午 4:53:59”。这是为什么?

UPDATE

更新

Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like

啊……很多人误会我了,我的不好,忘记指出要点了。我只用日期部分创建了日期,我不想要时间。所以我希望像

22 Jan 2011 12:00:00 am

回答by Salman A

You can call ->setTime(0, 0)to zero out the time portion:

您可以调用->setTime(0, 0)将时间部分归零:

$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am

回答by lonesomeday

See the documentation for DateTime::createFromFormat:

请参阅以下文档DateTime::createFromFormat

If formatdoes not contain the character ! then portions of the generated time which are not specified in formatwill be set to the current system time.

如果格式不包含字符!那么在格式中未指定的部分生成时间将被设置为当前系统时间。

If you do the following function call, you'll get the result you expect:

如果您执行以下函数调用,您将得到您期望的结果:

$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));

回答by Mircea Voivod

Today's start timestamp

今天的开始时间戳

$today_start_ts = strtotime(date('Y-m-d', time()). '00:00:00');

回答by naiquevin

You can do this by passing the current unix timestamp as the second parameter to the date function

您可以通过将当前的 unix 时间戳作为第二个参数传递给 date 函数来做到这一点

echo date("Y-m-d H:i:s",time());

回答by Xavier Barbosa

Remove this part g:i:s afrom your code.

g:i:s a从您的代码中删除这部分。

Now, if you want a nice date formatted according to your local, i recommand you to use strftime()function.

现在,如果您想要根据本地格式设置好的日期,我建议您使用strftime()函数。

回答by gion_13

You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have. I suggest you take a peak at the php datedocumentation.

您将收到“22 Jan 2011 4:53:59 pm”,因为这些是您格式化日期的规则:
d(日):22
M(月):Jan
Y(年):2011
g(12 小时格式) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
请更具体地说明您希望时间戳具有的格式。我建议你在php 日期文档中找到一个高峰。

回答by sdleihssirhc

Is it using UTC, or something?

是使用UTC还是什么?

I have a PHP version that gives me an error whenever I do something daterelated without first using date_default_timezone_set. Maybe that'll help you.

我有一个 PHP 版本,每当我在date没有首先使用date_default_timezone_set. 也许这会帮助你。