3

i am trying to get number of days between two given dates, but while trying this way its not giving the number of days.

$pur_dt = date_create('2015-08-03'); $todate = date_create(date('Y-m-d')); $diff = date_diff($todate,$pur_dt); print_r($diff); echo $diff->format('%R%a days'); if($diff>15) //checking condition if $pur_dt - $todate > 15 { echo 'Hello you are not eligible'; } else { echo 'eligible'; } 

its not working, not giving the number of days between given two dates.

1
  • try to replace your condition with if($diff->days > 15) Commented Feb 10, 2016 at 6:32

5 Answers 5

1

Try this. It is very simple.

 <?php $date1 = strtotime("2015-11-16 10:01:13"); $date2 = strtotime("2015-05-06 09:47:16"); $datediff = $date1 - $date2; echo floor($datediff/(60*60*24))." days"; //output 194 days ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Do *not use unix time. You'll be sorry. Try $NumberDays = gregoriantojd($EndM,$EndD,$EndY) - gregoriantojd($StartM,$StartD,$StartY); (for an inclusive range).
1

It's better using DateTime class, you can see comment(9) at PHP manual as it answer your question

Comments

0

Try this,

 $pur_dt = date_create('2015-08-03'); $todate = date_create(date('Y-m-d')); $datediff = $pur_dt - $todate; $diff = $datediff/(60*60*24); if($diff>15) //checking condition if $pur_dt - $todate > 15 { echo 'Hello you are not eligible'; } else { echo 'eligible'; } 

Comments

0

Try This :

$pur_dt = Date('2015-08-03'); $todate = Date(date('Y-m-d')); $pur_dt = strtotime($pur_dt); $todate = strtotime($todate); $seconds_diff = $todate - $pur_dt; $$diff = floor($seconds_diff/(60*60*24)); if($diff>15) //checking condition if $pur_dt - $todate > 15 { echo 'Hello you are not eligible'; } else { echo 'eligible'; } 

1 Comment

please use $diff on place of $$diff only remove first $
0

Try this

$pur_dt = date_create('2015-08-03'); $todate = date_create(date('Y-m-d')); $diff = date_diff($todate,$pur_dt); print_r($diff); echo $diff->format('%R%a days'); if($diff->days>15) //checking condition if $pur_dt - $todate > 15 { echo 'Hello you are not eligible'; } else { echo 'eligible'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.