简体   繁体   English

在PHP中的两个日期上获取年,月,周,日,时,分,秒间隔

[英]Get the year, month week, day, hour, minute, second interval on two dates in PHP

How can I get the year, month week day, hour, minute, second interval on two dates on php? 如何在php上的两个日期上获取年,月,周,日,时,分,秒间隔?

For example: 例如:

date1 = 2015-9-24 date2 = 2015-12-25 date1 = 2015-9-24 date2 = 2015-12-25

Output: 输出:

3 months and 1 day left

Thanks guys. 多谢你们。

Please try php diff 请尝试php diff

$date1 = new DateTime("2015-9-24");
$date2 = new DateTime("2015-12-25");
$interval = $date1->diff($date2);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 
echo "Result " . $interval->days . " days ";

Try this 尝试这个

$a1 = new DateTime('2015-9-24');
$a2 = new DateTime('2015-12-25');

$interval = $a2->diff($a1);

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

You are use PHP date_diff() Function 您正在使用PHP date_diff()函数

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM