PHP 获取距开始日期 31 天的距离

发布于 2024-09-26 16:53:13 字数 82 浏览 0 评论 0原文

如何获取以 $startDate 开头的 31 天后的日期,其中 $startDate 是以下格式的字符串:YYYYMMDD。

谢谢。

How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

苏辞 2024-10-03 16:53:13

strtotime 将为您提供一个 Unix 时间戳:

$date = '20101007';
$newDate = strtotime($date.' + 31 days');

然后您可以使用 date 将其格式化为相同的格式(如果您需要的话):

echo date('Ymd', $newDate);

strtotime will give you a Unix timestamp:

$date = '20101007';
$newDate = strtotime($date.' + 31 days');

you can then use date to format that into the same format, if that's what you need:

echo date('Ymd', $newDate);
記憶穿過時間隧道 2024-10-03 16:53:13

如果您使用 PHP 5.3:

$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');

至少可以说,缺少 5.3 之前的日期函数。 DateTime 使得处理日期变得更加容易。 https://www.php.net/manual/en/book.datetime。 php

If you're using PHP 5.3:

$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');

The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. https://www.php.net/manual/en/book.datetime.php

旧城空念 2024-10-03 16:53:13

请注意,如果您希望下个月的日期相同,而不是每次都精确到 31 天,+1 个月也可以。

Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.

梦里南柯 2024-10-03 16:53:13
echo date('Y m d',strtotime('+31 Days'));
echo date('Y m d',strtotime('+31 Days'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文