从天数计算月数

时间:2016-04-08 17:12:33

标签: php

这是一个非常简单的问题,但我无法找到解决方案。我现在已经在互联网上挖了好几天才找到合适的解决方案。我知道解决方案必须简单,但我只是想不出来。

        $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;

我正在计算今天和帐户过期之日的日期差异。如果帐户尚未过期,我会获得+值,如果帐户过期,则会获得-值。然后我将其转换为integet,然后如果小于0,我需要计算月数,以便我可以相应地计算客户的费用。在我正在开发的应用程序中,一个月是精确的30 days

它应该给出如下值

    if $days = 20 then $months = 1 // from 1 - 30, it should give 1
    if $days = 35 then $months = 2 // from 31 - 60, it should give 2 

等等。

编辑:

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

1 个答案:

答案 0 :(得分:0)

就像运行这样的东西一样容易。

$date = "April";
$days_in_month = date("t", strtotime($date));

$date是您想要的月份的字符串表示形式。并且strtotime会将其转换为时间戳(在此示例中为1460073600)。然后,您可以使用date并使用t设置,它将返回给定月份中的日期。此示例将在变量30

中为您提供$days_in_month

对于$date,您可以使用任何日期表示。例如,Feb 2016作为日期将为您提供29天......但Feb 2015将为您提供28

相关问题