[英]Subtracting days from a future date
$tour_date = '11 December 2022';
$newdate = date('j F, Y', strtotime('-10 day', strtotime($tour_date)));
echo 'Make payment until $newdate';
It is working as expected.它按预期工作。 $newdate prints as 1 December 2022
$newdate打印为 2022 年 12 月 1 日
However, if the $tour_date is not the current year, it does not work properly.但是,如果 $tour_date 不是当前年份,则它不能正常工作。 It is still printing as 1 December 2022.
截至 2022 年 12 月 1 日,它仍在印刷。
$tour_date = '11 December 2023';
$newdate = date('j F, Y', strtotime('-10 day', strtotime($tour_date)));
echo 'Make payment until $newdate';
The $newdate prints as 1 December 2022. But it should be 1 December 2023 which is -10 days from 11 December 2023. $newdate打印为 2022 年 12 月 1 日。但它应该是 2023 年 12 月 1 日,即从 2023 年 12 月 11 日开始的 -10 天。
Any idea, that will work with the current year and also future dates?任何想法,这将适用于当前年份和未来日期?
Edit:编辑:
My bad that I did not mention the date is actually like this: $tour_date = '11 December, 2023';我没有提到日期的糟糕之处实际上是这样的:$tour_date = '11 December, 2023';
Actually, the app Grammarly removed the comma when I submit the question.实际上,当我提交问题时,应用程序 Grammarly 删除了逗号。
Luckily, @Rylee read my comments and found the problem.幸运的是,@Rylee 阅读了我的评论并发现了问题。 Thank you very much!
非常感谢!
You mentioned in a comment that your date is stored as 11 December, 2023
.您在评论中提到您的日期存储为
11 December, 2023
。
The comma ,
is preventing PHP from parsing the date string correctly.逗号
,
阻止 PHP 正确解析日期字符串。 Remove the comma (using str_replace
or similar) and try again.删除逗号(使用
str_replace
或类似方法)并重试。
echo date("Y-m-d", strtotime("-10 day", strtotime("11 December 2023"))); // 2023-12-01
echo date("Y-m-d", strtotime("-10 day", strtotime("11 December, 2023"))); // 2022-12-01
In the case with the comma ,
it can't determine the year correctly so it defaults to this year.在逗号的情况下
,
它无法正确确定年份,因此默认为今年。
After more testing - there seems to be some weird results with that format.经过更多测试 - 这种格式似乎有一些奇怪的结果。 I'm not sure why this is exactly:
我不确定为什么会这样:
// Using date("Y-m-d", strtotime($input));
11 December, 1970 -> 1970-12-11
11 December, 1999 -> 1999-12-11
11 December, 2000 -> 2022-12-11
11 December, 2001 -> 2022-12-11
11 December, 2020 -> 2022-12-11
11 December, 2059 -> 2022-12-11
11 December, 2060 -> 2060-12-11
11 December, 2099 -> 2099-12-11
It seems that the "year" values after the comma have a special case between 2000 and 2059 (inclusive).逗号后面的“年份”值似乎在 2000 和 2059(含)之间有特殊情况。
echo 'Make payment until $tour_date (1 December 2022)'; echo '付款至 $tour_date (2022 年 12 月 1 日)';
Surely it should echo $newdate?当然它应该回显 $newdate 吗?
This:这个:
$tour_date = '11 December 2023';
$newdate = date(
'j F, Y',
strtotime(
'-10 days',
strtotime($tour_date)
)
);
echo "Make payment until $newdate";
outputs:输出:
Make payment until 1 December, 2023
Notice that I replaced '-10 day'
with '-10 days'
.请注意,我将
'-10 day'
替换为'-10 days'
。 And I used "
instead of '
when I used echo
. Nevertheless, using '-10 day'
also worked for me… I suspect you made a mistake when you copied the code here. Originally, your code here had:当我使用
echo
时,我使用了"
而不是'
。不过,使用'-10 day'
也对我有用......我怀疑你在这里复制代码时犯了一个错误。最初,你的代码在这里有:
echo 'Make payment until $tour_date (1 December 2022)';
and it was replaced with:它被替换为:
echo 'Make payment until $newdate';
You could also have done something like this:你也可以这样做:
$tour_date = '11 December 2023';
$newdate = date('j F, Y', strtotime("-10 days $tour_date"));
echo "Make payment until $newdate";
Or:或者:
$newdate = (new DateTimeImmutable('11 December 2023'))
->sub(new DateInterval('P10D'))
->format('j F, Y');
echo "Make payment until $newdate";
The code works as expected.该代码按预期工作。 Try changing the year on
$tour_date
string尝试更改
$tour_date
字符串上的年份
$tour_date = '11 December 2022';
PHP is cursed and nobody should be using it, in my opinion at least. PHP 被诅咒了,至少在我看来,没有人应该使用它。 Either way, here's a working example of that which you intended to achieve:
无论哪种方式,这是您打算实现的工作示例:
<?php
$originalDate = "2023-12-11";
$daysToSubtract = 10;
$date=date_create($originalDate);
$subtracteddate = date_sub($date,date_interval_create_from_date_string($daysToSubtract . " days")); ###Could be " months", or " seconds"
echo "Original Date: " . date_format($date,"Y-m-d") . "\r\r";
echo "Original minus " . $daysToSubtract . " Days: " . date_format($subtracteddate,"Y-m-d");
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.