简体   繁体   中英

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?

For example:

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

Output:

3 months and 1 day left

Thanks guys.

Please try 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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