在php中添加六个月 [英] Add six months in php

查看:89
本文介绍了在php中添加六个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从当前日期起6个月。

I'm trying to get the month, six months out from the current date.

我尝试使用:

date('d',strtotime '+6个月',time()));

但是似乎不起作用,总是返回 01 。有没有更好的方法?

But it doesn't seem to work, always returns 01. Is there a better way to do this?

谢谢!

推荐答案

p>我发现使用 DateTime 更容易使用:

I find working with DateTime much easier to use:

$datetime = new DateTime();
$datetime->modify('+6 months');
echo $datetime->format('d');

$datetime = new DateTime();
$datetime->add(new DateInterval('P6M'));
echo $datetime->format('d');

或在PHP版本5.4 +

or in PHP version 5.4+

echo (new DateTime())->add(new DateInterval('P6M'))->format('d');

这篇关于在php中添加六个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆