0

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?

2
  • 5
    You're converting $start to a unix timestamp, but not $end: if($end){ $now = strtotime($end); }else{ $now = time(); }` Commented Sep 12, 2013 at 10:36
  • $now=$end should $now=strtotime($end); Commented Sep 12, 2013 at 10:39

2 Answers 2

1

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; 
Sign up to request clarification or add additional context in comments.

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.