Finding the number of days between two dates

2018-12-31 03:09发布

How to find number of days between two dates using PHP?

标签: php
26条回答
只靠听说
2楼-- · 2018-12-31 04:04

I'm using Carbon in my composer projects for this and similar purposes.

It'd be as easy as this:

$dt = Carbon::parse('2010-01-01');
echo $dt->diffInDays(Carbon::now());
查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 04:06
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>

used the above code very simple. Thanks.

查看更多
人气声优
4楼-- · 2018-12-31 04:06

If you are using MySql

function daysSince($date, $date2){
$q = "SELECT DATEDIFF('$date','$date2') AS days;";
$result = execQ($q);
$row = mysql_fetch_array($result,MYSQL_BOTH);
return ($row[0]);

}

function execQ($q){
$result = mysql_query( $q);
if(!$result){echo ('Database error execQ' . mysql_error());echo $q;}    
return $result;

}

查看更多
心情的温度
5楼-- · 2018-12-31 04:07

Used this :)

$days = (strtotime($endDate) - strtotime($startDate)) / (60 * 60 * 24);
print $days;

Now it works

查看更多
笑指拈花
6楼-- · 2018-12-31 04:11

Object oriented style:

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

Procedural style:

$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
查看更多
流年柔荑漫光年
7楼-- · 2018-12-31 04:11

Try using Carbon

$d1 = \Carbon\Carbon::now()->subDays(92);
$d2 = \Carbon\Carbon::now()->subDays(10);
$days_btw = $d1->diffInDays($d2);

Also you can use

\Carbon\Carbon::parse('')

to create an object of Carbon date using given timestamp string.

查看更多
登录 后发表回答