从 strtotime 到当前时间的 PHP 日期

发布于 2025-01-04 15:56:57 字数 313 浏览 0 评论 0原文

我一直在网上寻找这个答案,但结果是空的...我非常累,所以我想我应该尝试一下...

我有一个变量,其中包含文本框中的日期

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

看透却不说透 2025-01-11 15:56:57

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

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

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

echo $currentDate;

这从一种格式的变量中获取日期,并从另一种格式的另一个变量中获取日期,然后将它们放在一起:)

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

DateTime::createFromFormat

也可以工作,但是仅当您有 PHP 5.3 或更高版本时...(我认为)

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)

離殇 2025-01-11 15:56:57

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.

半边脸i 2025-01-11 15:56:57

您看到的结果是由于输入的日期不是 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 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.

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

苦笑流年记忆 2025-01-11 15:56:57

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

您可以使用日期时间对象。

面向对象的最佳方式是:

$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');

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:

$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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文