来自 strtotime 的 PHP 日期和当前时间

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

PHP Date from strtotime with current time

phpdatestrtotime

提问by user979331

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

当我回应这一点时,我得到 1969-12-31 19:00:00

Is this possible? Can someone point me in the right direction?

这可能吗?有人可以指出我正确的方向吗?

回答by user979331

I found a solution to my problem....

我找到了解决我的问题的方法......

$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");

$currentDate =  date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));

echo $currentDate;

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.....

感谢大家的时间......

DateTime::createFromFormat

would also work but only if you have PHP 5.3 or higher...(I think)

也可以,但前提是你有 PHP 5.3 或更高版本......(我认为)

回答by David Barnes

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.

EffectiveDate 字符串不是 strtotime 识别的格式,因此 strtotime 返回 false ,它被解释为 0,这导致日期显示为 1970 年 1 月 1 日 00:00:00,减去您的时区偏移。

回答by Niet the Dark Absol

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 confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.

您看到的结果是由于输入的日期不是strtotime. 在不知道您使用的格式的情况下,我能想到的最可能的情况是您使用了美国的顺序,将月份和日期以错误的方式放置 - 这strtotime令人困惑,因为如果它同时接受,则无法区分 2 月 3 日和 3 月第二,所以它必须拒绝美国格式的日期。

The most reliable format for strtotimeis YYYY-MM-DD HH:ii:ss, as it is unambigous.

最可靠的格式strtotime是 YYYY-MM-DD HH:ii:ss,因为它是明确的。

回答by Marcos Fedato

The date is just a timestamp, it is not object-oriented and i don't like it.

日期只是一个时间戳,它不是面向对象的,我不喜欢它。

You can use the DateTime object.

您可以使用 DateTime 对象。

The object-oriented best way is:

面向对象的最佳方式是:

$effectiveDate=$_REQUEST['effectiveDate'];

// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);

// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');