Php 从天数计算月数

Php 从天数计算月数,php,Php,这是一个相当简单的问题,但我找不到解决办法。为了找到一个合适的解决方案,我已经在互联网上搜寻了好几天了。我知道解决办法一定很简单,但我就是想不出来 $datetimeToday = new DateTime(date('d M Y')); $datetimeAccountExpirationDate= new DateTime($account->expiration_date); $interval = $datetimeToday->

这是一个相当简单的问题,但我找不到解决办法。为了找到一个合适的解决方案,我已经在互联网上搜寻了好几天了。我知道解决办法一定很简单,但我就是想不出来

        $datetimeToday = new DateTime(date('d M Y'));
        $datetimeAccountExpirationDate= new DateTime($account->expiration_date);
        $interval = $datetimeToday->diff($datetimeAccountExpirationDate);
        $days = $interval->format('%R%a');
        $days = intval($days);

        if($days <= 0):
            // calculate the number of months, a month is of 30 days (for this app)
        endif;
等等

编辑:

var_dump($datetimeToday->diff($datetimeAccountExpirationDate)->d); // this would give me days
var_dump($datetimeToday->diff($datetimeAccountExpirationDate)->m); // this would return 0 for days > 0 but < 30
var_dump($datetimeToday->diff($datetimeAccountExpirationDate)->d);//这会给我几天的时间
var_dump($datetimeToday->diff($datetimeAccountExpirationDate)->m);//如果天数大于0但小于30,则返回0

运行这样的程序很简单

$date = "April";
$days_in_month = date("t", strtotime($date));
$date
是所需月份的字符串表示形式。而
strotime
将其转换为时间戳(在本例中为1460073600)。然后您可以使用
date
并使用
t
设置,它将返回给定月份的天数。本例将在变量
$days\u in\u month
中为您提供
30


对于
$date
,可以使用日期的任何表示形式。例如,
2016年2月
,因为该日期将为您提供
29天
。。。但是
2015年2月
会给你
28天

我建议你熟悉
strotime()
date()
如果你不在乎月份是30天还是31天,二月是特殊的,你只需要使用一个简单的模就可以了。。。
$date = "April";
$days_in_month = date("t", strtotime($date));