danyo
danyo

Reputation: 5856

php counting between dates

I have the following code 2 count days between 2 dates:

 $start = '2013-04-02'; 
 $end = '';
 if($end){ 
 $now = $end; 
 }else{
 $now = time();
 }
 $your_date = strtotime($start);
 $datediff = $now - $your_date;
 echo floor($datediff/(60*60*24));

This works fine and calculates the correct amount of days.

When i enter a end date like this:

 $start = '2013-04-02'; 
 $end = '2013-09-11';
 if($end){ 
 $now = $end; 
 }else{
 $now = time();
 }
 $your_date = strtotime($start);
 $datediff = $now - $your_date;
 echo floor($datediff/(60*60*24));

I get a result of -15797.

Does the above seem ok? Or am i doing something wrong?

Upvotes: 0

Views: 139

Answers (2)

Ujjwal Ojha
Ujjwal Ojha

Reputation: 1360

Try this::

$start    = new DateTime('2013-04-02');
$end      = new DateTime('2013-09-11');
$interval = DateInterval::createFromDateString('1 day');
$period   = new DatePeriod($start, $interval, $end);
$count=count($period);
echo $count; 

Upvotes: 1

Adam
Adam

Reputation: 1371

shoud be:

 $start = '2013-04-02'; 
 $end = '2013-09-11';
 if($end){ 
 $now = strtotime($end); 
 }else{
 $now = time();
 }
 $your_date = strtotime($start);
 $datediff = $now - $your_date;
 echo floor($datediff/(60*60*24));

Upvotes: 0

Related Questions