二月和strtotime [英] February and strtotime

查看:138
本文介绍了二月和strtotime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以吗?

我试图在这里打个计算日期,但是我没有得到..

I'm trying to hit a calculation date here, however I am not getting ..

发生以下情况:

$AtualMenos3 = date("m-d-Y", strtotime("2013-03-31 -3 month"));
$AtualMenos2 = date("m-d-Y", strtotime("2013-03-31 -2 month"));
$AtualMenos1 = date("m-d-Y", strtotime("2013-03-31 -1 month"));
$AtualMes = date("m-d-Y", strtotime("2013-03-31"));

正确的应该是:

03-31-2013
02-28-2013
01-31-2013
12-31-2012

但是,这个php计算日期错误,因为当月份是二月(只有28天)时,计算错误。

However, this php calculating the date wrong, because when month is February (which only has 28 days) the calculation goes wrong ..

实际输出:

03-31-2013
03-03-2013
01-31-2013
12-31-2012

有人知道我如何解决这个问题吗?因为这很复杂..我没有想法...我想不出什么:\

Does anyone know give me a light on how I fix this? because this complicated .. I'm out of ideas ... I can not think of anything: \

推荐答案

    $datetime = new DateTime('2013-03-31');
    $datetime->modify('-1 month');
    echo $datetime->format('Y-m-t') . "<br>";
    $datetime->modify('-1 month');
    echo $datetime->format('Y-m-t') . "<br>";
    $datetime->modify('-1 month');
    echo $datetime->format('Y-m-t') . "<br>";

看到它在行动

这篇关于二月和strtotime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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