从 strtotime 到当前时间的 PHP 日期
我一直在网上寻找这个答案,但结果是空的...我非常累,所以我想我应该尝试一下...
我有一个变量,其中包含文本框中的日期
$effectiveDate=$_REQUEST['effectiveDate'];
我想要做什么是采用此日期并添加当前时间
date('Y-m-d H:i:s', strtotime($effectiveDate))
当我回显此内容时,我得到 1969-12-31 19:00:00
这可能吗?有人能指出我正确的方向吗?
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了解决问题的方法......
这从一种格式的变量中获取日期,并从另一种格式的另一个变量中获取日期,然后将它们放在一起:)
感谢大家的时间......
也可以工作,但是仅当您有 PHP 5.3 或更高版本时...(我认为)
I found a solution to my problem....
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
would also work but only if you have PHP 5.3 or higher...(I think)
effectiveDate 字符串不是 strtotime 可以识别的格式,因此 strtotime 返回 false,它被解释为 0,这会导致日期显示为 1970 年 1 月 1 日的 00:00:00,减去您的时区偏移量。
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
您看到的结果是由于输入的日期不是
strtotime
识别的格式造成的。在不知道您使用的格式的情况下,我能想到的最有可能的情况是您使用了美国顺序,将月份和日期放在错误的位置 - 这使strtotime
感到困惑,因为如果它接受两者,那么它无法区分 2 月 3 日和 3 月 2 日,因此它必须拒绝美国格式的日期。strtotime
最可靠的格式是 YYYY-MM-DD HH:ii:ss,因为它是明确的。The result you see is caused by the entered date not being in a format recognised by
strtotime
. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confusesstrtotime
, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.The most reliable format for
strtotime
is YYYY-MM-DD HH:ii:ss, as it is unambigous.日期只是一个时间戳,它不是面向对象的,我不喜欢它。
您可以使用日期时间对象。
面向对象的最佳方式是:
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is: