草庐IT

php - 在午夜获取今天的时间戳?

coder 2023-04-24 原文

我将如何在 php 中获取今天午夜的时间戳。假设现在是星期一下午 5 点,我想要星期一(今天)午夜(上午 12 点)的时间戳。

谢谢

最佳答案

$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();
  1. 即在 default timezone 中。
  2. 剧透:仅“midnight”或“today”返回相同。
  3. 添加时区,例如“UTC today ”,永远拥有它。
  4. 剧透 #2:UTC 问候风格:“midnightZ
  5. 历史记录: midnight PHP 5.1.2(2006 年 1 月)起, today PHP 4.3.1(2003 年 2 月)起

更多例子:

鉴于时间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 文档:

  • Relative Formats 页面上,查看基于日的表示法表。这些格式适用于 strtotime() DateTime date_create()

  • 您可能想看看 PHP 提供的更多功能:https://php.net/datetime - PHP 中与日期时间相关的函数和对象的入口页面,以及指向其他与日期时间相关的扩展的链接。


注意:虽然“midnight”在技术上两天之间,但这里是“today”(一天的开始)和 PHP 的 strtotime

讨论:

到目前为止,答案 strtotime("today midnight")strtotime("midnight today") 是 PHP 的一个完整且听起来不错的答案,它可能看起来有点冗长,因为 strtotime("midnight")strtotime("today") 返回相同的结果。

但即使是更冗长也不总是能立即回答这个问题,如果它是关于一天开始的午夜或一天结束的午夜,即使在今天也被给出为语境。阅读“今天午夜”时,我们可能会想到一天的开始,但这是一个假设,并不精确,也许不可能。 Wikipedia:

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 时间戳之间,因此您将获取两个时间戳并描述两者的含义,这不会回答问题,因为它要求一个时间戳)。

由于这种不匹配以及 UNIX 时间引用日期/时间的方式,这不容易完全解决。

让我们用众所周知的数字 24 小时时钟来表示小时和分钟并表示午夜(更精确吗?):

$midnight = strtotime("today 00:00");

或结束一天:

$midnight = strtotime("today 24:00");

(注意:显示为第二天的开始)

或者12小时制,也可以用来给出今天午夜UNIX时间戳:

$midnight = strtotime("12:00 a.m.");

(注意:strtotime() 不支持 “午夜 12 点” 表示法)

转换时间(如 Midnight)在时钟上映射可能会导致混淆,使用类似于“midnight”的 12 小时制时钟甚至会变得更加明显midday(在 PHP 中不能作为相对日期/时间格式,但“noon”)在技术上介于 again 之间(现在在中午的两个完整日期之间,或在第一个日期之间和后半天)。

这样一来,代码在 24 小时内可能不会被很好地接受,或者只是写出“today midnight”。

您的里程可能会有所不同。

这有点与时钟时间一致。来自 Wikipedia Midnight :

"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]"

并从 Wikipedia 12-hour clock :

"It is not always clear what times "12:00 a.m." and "12:00 p.m." denote. From the Latin words meridies (midday), ante (before) and post (after), the term ante meridiem (a.m.) means before midday and post meridiem (p.m.) means after midday. Since "noon" (midday, meridies (m.)) is neither before nor after itself, the terms a.m. and p.m. do not apply. Although "12 m." was suggested as a way to indicate noon, this is seldom done and also does not resolve the question of how to indicate midnight."

关于php - 在午夜获取今天的时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13129817/

有关php - 在午夜获取今天的时间戳?的更多相关文章

  1. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  2. ruby - 简单获取法拉第超时 - 2

    有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url

  3. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  4. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  5. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  6. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  7. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  8. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

  9. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  10. ruby - 没有类方法获取 Ruby 类名 - 2

    如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象

随机推荐