在午夜获取今天的时间戳?
我将如何在 php 中获取今天午夜的时间戳.假设现在是星期一下午 5 点,我想要星期一(今天)午夜(上午 12 点)的时间戳.
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.
谢谢
推荐答案
$timestamp = strtotime('today midnight');
或通过日期时间:
$date = new DateTime('today midnight');
// or: $date = date_create('today midnight');
$timestamp = $date->getTimestamp();
然后也许是不可变的:
$midnight = new DateTimeImmutable('today midnight');
// or: $midnight = date_create_immutable('today midnight');
$timestampOfMidnight = $midnight->getTimestamp();
- 那是在 默认时区.
- 剧透:只是
午夜
"或或只是today
"返回相同. - 添加时区,例如
UTC today
",永远拥有它. - 剧透 #2:UTC 问候风格:
midnightZ
" - 历史:
midnight
自 PHP 5.1.2(2006 年 1 月),今天
自 PHP 4.3.1(2003 年 2 月)
- That is in the default timezone.
- Spoiler: just "
midnight
" or or just "today
" return the same. - Add a timezone, e.g. "
UTC today
", to have it, always. - Spoiler #2: UTC greeting style: "
midnightZ
" - History:
midnight
is since PHP 5.1.2 (Jan 2006),today
since PHP 4.3.1 (Feb 2003)
更多例子:
鉴于时间UTC 2020-01-01 00:00:00:
UTC time is ............: [red ] 1577836800
调用strtotime($)
时,结果为:
today midnight .........: [pink ] 1577833200
midnight ...............: [pink ] 1577833200
today ..................: [pink ] 1577833200
tomorrow ...............: [green ] 1577919600
UTC today ..............: [red ] 1577836800
today Z ................: [red ] 1577836800
Asia/Shanghai today ....: [lime ] 1577808000
Asia/Shanghai ..........: [blue ] 1577811600
HKST today .............: [copper] 1577804400
在线演示:https://3v4l.org/KWFJl
PHP 文档:
关于相对格式 页面,请参阅 Day-based Notations 表.这些格式适用于
strtotime()
、DateTime
和date_create()
.
On the Relative Formats page, see the Day-based Notations table. These formats are for
strtotime()
,DateTime
anddate_create()
.
您可能想看看 PHP 提供的更多功能:https://php.net/datetime - PHP 中与日期时间相关的函数和对象的入口页面,带有指向其他日期时间相关扩展的链接.
You might want to take a look what more PHP has to offer: https://php.net/datetime - the entry page to date-time related functions and objects in PHP with links to other, date-time related extensions.
注意:在midnight
"期间在技术上 两天之间,这里是today
"(一天的开始)使用 PHP 的 strtotime
.
NOTE: While "midnight
" being technically between two days, here it is "today
" (start of day) with PHPs' strtotime
.
讨论:
到目前为止,答案 strtotime(today午夜")
或 strtotime(midnight today")
对于 PHP 来说是一个完整且听起来不错的答案,它可能看起来有点冗长,因为 strtotime("midnight")
和 strtotime("today")
返回相同的结果.
In so far, the answer strtotime("today midnight")
or strtotime("midnight today")
is a complete and well sounding answer for PHP, it may appear a bit verbose as strtotime("midnight")
and strtotime("today")
return the same result.
但即使是更冗长也不总是能立即回答这个问题,如果它是关于一天开始的午夜或一天结束的午夜,即使在今天也被给出为语境.阅读今天午夜"时,我们可能会想到一天的开始,但这是一个假设,并不精确,也许不可能.维基百科:
But even being more verbose not always instantly answers the question if it is about the Midnight for start of day or the Midnight for end of day even today is given as context. We may think about the start of day when reading "today midnight", but this is an assumption and not precise and perhaps can't be. Wikipedia:
尽管在这个问题上没有全球一致意见,但午夜通常被认为是新一天的开始,并与 00:00 小时相关联.
Though there is no global unanimity on the issue, most often midnight is considered the start of a new day and is associated with the hour 00:00.
与这个编程问题比较:
- 鉴于午夜是一个时间转换
- 要求单个 UNIX 时间戳时
- 那就没有答案了
(它将在两个 UNIX 时间戳之间,因此您将获取两个时间戳并描述两者的含义,这不会回答问题,因为它要求一个时间戳).
(it would be between two UNIX timestamps, so you would take two timestamps and describe what the two mean and this would not answer the question as it asks for a single timestamp).
由于这种不匹配以及 UNIX 时间引用日期/时间的方式,这不容易完全解决.
This is not easy to completely resolve because of this mismatch and by how UNIX Time references date/time.
让我们用众所周知的数字 24 小时时钟来表示小时和分钟并表示午夜(更精确吗?):
Lets take the well known, digital 24-hour clock with hours and minutes and express midnight (is it more precise?):
$midnight = strtotime("today 00:00");
或结束一天:
$midnight = strtotime("today 24:00");
(注意:显示为第二天的开始)
(NOTE: shown as start of next day)
或者12小时制,也可以用来给出今天午夜的UNIX时间戳:
Or the 12-hour clock, it can also be used to give the UNIX Timestamp of today at midnight:
$midnight = strtotime("12:00 a.m.");
(注意:strtotime()
不支持 午夜 12 点" 表示法)
(NOTE: the "12 midnight" notation is not supported by strtotime()
)
过渡时间(如午夜)映射到时钟上可能会导致混淆,使用类似于<代码>午夜代码>"midday(在 PHP 中不能作为相对日期/时间格式,但noon
")在技术上介于 again 之间(现在介于两个中午的完整日期,或在一天的前半天和后半天之间).
The confusion that can result of a transition time like Midnight to map on a clock may even become more visible with the 12-hour clock as similar to "midnight
" the midday (not available in PHP as a relative date/time format but "noon
") is technically between again (now between two full dates at noon, or between the first and the second half of a day).
随着时间的推移,代码在 24 小时内可能不会被很好地接受,或者只是写出today午夜
".
As this adds up, the code is likely not well received over a 24 hour clock or just writing out "today midnight
".
您的里程可能会有所不同.
Your mileage may vary.
这有点与时钟时间一致.来自维基百科午夜:
This is kind of aligned with clock time. From Wikipedia Midnight:
午夜是从一天到下一天的过渡时间——日期变化的时刻,在任何特定司法管辖区的当地官方时钟时间.按时钟时间,午夜与中午相反,相差 12 小时.[被我加粗]"
"Midnight is the transition time from one day to the next – the moment when the date changes, on the local official clock time for any particular jurisdiction. By clock time, midnight is the opposite of noon, differing from it by 12 hours. [bold by me]"
并且来自 维基百科 12 小时制:
and from the Wikipedia 12-hour clock:
并不总是很清楚12:00 a.m."的时间.和下午 12:00"表示.从拉丁词 meridiem(中午)、ante(之前)和 post(之后)中,ante meridiem (a.m.) 表示中午之前,post meridiem (p.m.) 表示中午之后.从中午"开始(midday, meridies (m.)) 既不在其自身之前也不在其之后,即 a.m. 和 p.m. 这两个术语.请勿应用.虽然12 米".被建议作为一种表示中午的方式,但很少这样做,也不能解决如何表示午夜的问题."
相关文章