PHP两个日期之间月份的差额? [英] PHP Difference in months between two dates?

查看:89
本文介绍了PHP两个日期之间月份的差额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何使用PHP计算两个日期之间的差?/a>
php中的日期差异?

Possible Duplicate:
How to calculate the difference between two dates using PHP?
Date Difference in php?

我在像这样的变量中有两个日期

I have two dates in a variable like

$fdate = "2011-09-01"

$ldate = "2012-06-06"

现在我需要它们之间相差几个月的时间.
例如,如果您从明年的第9个月(9月)到次年6月06(六月)计算得出答案,则答案应该为10.结果将是10.
如何在PHP中做到这一点?

Now I need the difference in months between them.
For example, the answer should be 10 if you calculate this from month 09 (September) to 06 (June) of next year - you'll get 10 as result.
How can I do this in PHP?

推荐答案

更优雅的解决方案是使用 DateTime DateInterval .

A more elegant solution is to use DateTime and DateInterval.

<?php

// @link http://www.php.net/manual/en/class.datetime.php
$d1 = new DateTime('2011-09-01');
$d2 = new DateTime('2012-06-06');

// @link http://www.php.net/manual/en/class.dateinterval.php
$interval = $d2->diff($d1);

$interval->format('%m months');

这篇关于PHP两个日期之间月份的差额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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