0

I've one mysql table in that I store two dates say 2015-03-13 and 2015-03-20 and I displays these dates on form with no. of days between these two dates.

In this I want PHP code for calculating days.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Vg.
  • 133
  • 2
  • 3
  • 17
  • 1
    Welcome to SO. Kindly have a [**tour**](http://stackoverflow.com/tour) to SO & come back again with your efforts & specific problem you face. – Rikesh Mar 13 '15 at 05:28
  • 1
    Always do a google search before asking, possible duplicate of http://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates – mlegge Mar 13 '15 at 05:28

5 Answers5

0

Try with this (Object oriented style):

<?php
$datetime1 = new DateTime('2015-03-13');
$datetime2 = new DateTime('2015-03-20');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a días');
?>

Procedural Style:

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a días');
?>

You can read more at:

http://php.net/manual/en/datetime.diff.php

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0
$start = strtotime('2015-03-13');
$end = strtotime('2015-03-20');

$diff = $end - $start;

$days = floor($diff / (3600 * 24));
Tech Savant
  • 3,686
  • 1
  • 19
  • 39
0

Try This.

$date1=date_create("2013-03-15");<br>
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2); <br>
echo $diff->format("%R%a days");

Source: http://www.w3schools.com/php/showphp.asp?filename=demo_func_date_diff

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

Try this code:

<?php
     $first_date = strtotime("2015-03-10");
     $last_date = strtotime("2015-03-01");
     $datediff = $first_date - $last_date;
     echo "Days : ". floor($datediff/(60*60*24));
?>
AkshayP
  • 2,141
  • 2
  • 18
  • 27
0

Use this

   $daylen = 60*60*24;
   $date1 = '2015-03-13';
   $date2 = '2015-03-20';    
   echo (strtotime($date1)-strtotime($date2))/$daylen;
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41