1

I need to calculate number of days between two dates. Required date entered by me,fetch the record from the database by the given dates. If the database have 'startdate' as 1Jan2015 'enddate' as 5Feb2015. For January month it should return 30 and for February 5 days.

My table:

id  Name    Type    Project Place            Start Date              End Date                Details
1   Sai Local   Site    Bangalore   2015-09-03 11:32:47 2015-09-05 11:32:47 test                    
2   Ram Local   IGCAR   Chennai     2015-04-01 15:15:36 2015-04-09 15:15:36 Installation                    
3   Mani    Local   IGCAR   Chennai     2015-04-16 15:16:18 2015-05-21 15:16:18 Training

My coding

///////////Employee Outstation(Travel) details/////////////
    $employeeTravel = new EmployeeTravelRecord();
    //date_start = '2015-04-01' ;
    //date_end   = '2015-04-30';

    $TravelEntryList = $employeeTravel->Find("(travel_date between ? and ? or  return_date between ? and ? )",array($req['date_start'], $req['date_end'],$req['date_start'], $req['date_end']));
    foreach($TravelEntryList as $Travelentry){

    $amount = (strtotime($Travelentry->return_date) - strtotime($Travelentry->travel_date));

                }   

For second record, it returns correct value, but for third record it calculates including May month. But i want only 30 days of april.

Anu
  • 905
  • 4
  • 23
  • 54
  • here is a [daysAgo visual](http://stackoverflow.com/a/32753903) from yesterday you can gander at, while waiting for your code drop – Drew Sep 25 '15 at 05:50

3 Answers3

0

DATEDIFF() returns value in days from one date to the other.

select *,datediff( end Date,  Start Date) as days from My table;
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0

Please have a look at this post, you should find what you're looking for :

How to get the number of days of difference between two dates on mysql?

Community
  • 1
  • 1
Basile
  • 326
  • 1
  • 7
  • In this,its calculate number of days between two dates with in the same month in database.But in my case, one record has april and may month in start and end date. In that how can i use DateDiff() only for April month. – Anu Sep 25 '15 at 09:57
  • not sure to understand what you mean : DateDiff() will work even if both dates aren't within the same month (**example** : _select datediff('2011-05-15', '2010-04-12');_ will return 398 Days) – Basile Sep 25 '15 at 16:38
  • Yes , actually i want to get how many days in may month, ('2011-05-15', '2010-04-12');Here in may month,he is out-of-station for 15 days and in April 19days..i want to calculate for each month between these two dates,how many days per month. – Anu Sep 28 '15 at 05:42
0

There is a function in PHP called as date_diff for difference between two dates.

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

?>
oreopot
  • 3,392
  • 2
  • 19
  • 28