Ashish
Ashish

Reputation: 129

How to find out the number of days between two dates

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.

Upvotes: 3

Views: 103

Answers (5)

Amit Visodiya
Amit Visodiya

Reputation: 813

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';
}

Upvotes: 0

budirec
budirec

Reputation: 288

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

Upvotes: 1

JavidRathod
JavidRathod

Reputation: 483

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';
}

Upvotes: 0

Amit Rajput
Amit Rajput

Reputation: 2059

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

    ?>

Upvotes: 1

itzmebibin
itzmebibin

Reputation: 9439

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';
     }

Upvotes: 0

Related Questions