在2个日期之间取得年数? [英] get years between 2 dates?
本文介绍了在2个日期之间取得年数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ive有两个日期,如:
2009-11-11
2002-11-11
我想要得到他们之间的'7'年。我该怎么办?月和日总是一样的,我不想使用 - 。是否有适合的方式?
解决方案
使用php 5.3+可以使用 DateTime :: diff()
$ a = new DateTime('2009-11-11');
foreach(array('2002-11-11','2002-11-12','2005-05-06')as $ dt){
$ b = new DateTime($ dt) ;
echo $ dt,'| ',$ a-> diff($ b) - >格式('%y'),'| ',$ a-> diff($ b) - > format('%y%m%d'),\\\
;
}
打印
2002-11-11 | 7 | 7 0 0
2002-11-12 | 6 | 6 11 29
2005-05-06 | 4 | 4 6 5
ive got 2 dates like:
2009-11-11 2002-11-11
and i want to get the years between them '7'. how should i do that? the month and day will always be identical and i dont want to use -. is there a appropiate way for this?
解决方案
With php 5.3+ you can use DateTime::diff()
$a = new DateTime('2009-11-11');
foreach( array('2002-11-11', '2002-11-12', '2005-05-06') as $dt) {
$b = new DateTime($dt);
echo $dt, ' | ', $a->diff($b)->format('%y'), ' | ', $a->diff($b)->format('%y %m %d'), "\n";
}
prints
2002-11-11 | 7 | 7 0 0
2002-11-12 | 6 | 6 11 29
2005-05-06 | 4 | 4 6 5
这篇关于在2个日期之间取得年数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文