PHPjabbers - tools for webmasters
shopping cart (0 item)
HOME
PRODUCTS
PHP TUTORIALS
PHP FORUM
ABOUT US
CONTACT US
Our products

days between two dates

Posted by Sparky on Jun 15th, 2006

hi, can you please tell me how to calculate number of days between two days using php. Thanks

Back
REPLIES
Posted by Vinay Sachan on Jan 15th, 2012


// different between two dates by date_diff() function:-
$datetime1 = date_create('2010-01-01');
$datetime2 = date_create('2012-1-15');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days')."
";
//+744 days
echo $interval->format('%y-%m-%d days')."
";
//2-0-14 days
// different between two dates by strtotime() function:-
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24)) ."
";
//744
?>

for more PHP questions and their best answers and PHP notes at http://onlinephpstudy.blogspot.com/
Posted by rajrndra babu on Oct 4th, 2011

i need difference between two date in day format . i my excel month/day/year format for example i) 4/25/2011 in one column & in another column 5/30/2011 , how to calculate in excel format ONLY
Posted by Praful Rajput on Aug 10th, 2011

Thanks!!!

Really very nice example and coding....


it save my lots of time....

Thanks
Praful
Posted by nani on Jun 4th, 2011


function dateDiff($start, $end) {
$start_ts = strtotime($start);
$end_ts = strtotime($end);
$diff = $end_ts - $start_ts;
return round($diff / 86400);
}
echo dateDiff("2011-03-07", "2011-06-06").'days';
Posted by Krisp PL on May 10th, 2011

Hey guys,

what you doing here is calculating how many 24hs (not days) is between 2 dates. Try to ask yourself how many days is between:
2011-04-30 22:00 and 2011-05-01 01:00?
1 day or 0 days :)))
Posted by Slavi on Mar 28th, 2011


$days = (strtotime(date("Y-m-d")) - strtotime("2005-11-20")) / (60 * 60 * 24);
print $days;


either use abs(); or in the order shown above because the result is negative.

Slavi
Posted by Dhananjay on Mar 2nd, 2011

Res./Sir,
please give me formula in excel how to calculate days between both date for cash discount in day wise
Posted by amit on Dec 18th, 2010

i want simple function which return no of days between two given days

any help will appreciable
Posted by MAsad on Oct 2nd, 2010

I want to calculate Number of days in each month betweeen two dates.
Posted by Komla on Sep 12th, 2010

Thank u Alexandar. You saved me much time !
Posted by muhammad on Aug 19th, 2010

I have a different problem. I want to compute the week number between any two dates e.g 1Apr - 31Mar
how to compute the week number between them
thanks in advacne
Posted by Ranga Pathmasiri on Jun 23rd, 2010

This is a great job Jackson . Thanks Jackson
Posted by AJay Bairagi on Jun 11th, 2010

I want to add two dates and add month.
Posted by siti on Mar 25th, 2010

i want to calculate for two different date.
the start_date i name it c_date(state for $commencement_date). the second one is $today. the result i name the variable is $service_year. but somebody told me, that my wrong is in i state that one year as 365 days, but not really are( for leap year is 366 right) so, how can i fix this problem?


$c_date = date('d-m-Y',strtotime($row1['commencement_date']));

$today = date ("d-m-Y");

$service_year = round(dateDiff("-", date("d-m-Y",time()), $c_date)/365,2);



(this is the function for convert year - year, month and date)
function year2monthsNdays($years)
{
$array = explode(".",$years);
$year = $array[0];
$month = ($array[1]>9) ? $array[1]/100 : $array[1]/10;
if ($month) {
$days = round($month*365,2);
$daysArray = explode(".",$days);
$months = round($daysArray[0]/30,2);
$monthArray = explode(".",$months);
$monthInt = $monthArray[0];
$daysInt = round($monthArray[1]*30/100,1);
}
$a = "$year-$monthInt-$daysInt";

return $a;
}

$service_year_convert = year2monthsNdays($service_year);
Posted by Jackson on Mar 5th, 2010


function getDaysInBetween($start, $end) {
// Vars
$day = 86400; // Day in seconds
$format = 'Y-m-d'; // Output format (see PHP date funciton)
$sTime = strtotime($start); // Start as time
$eTime = strtotime($end); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();

// Get days
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}

// Return days
return $days;
}



#####################| Print |####################

$sdate = "2010-03-5";
$edate = "2010-03-15";

$days = getDaysInBetween($sdate, $edate);

foreach ($days as $val)
{
echo $val."
";
}
Posted by adi on Jan 22nd, 2010


my query gvs this ans

name date
sandy 01/jan/2010
avi 02/jan/2010
sandy 02/jan/2010
sats 03/jan/2010
nil 03/jan/2010
sandy 03/jan/2010
avi 03/jan/2010
avi 04/jan/2010
sandy 05/jan/2010

& i want ot print in this manner

name/date 01/jan/2010 02/jan/2010 03/jan/2010
avi 0 1 1
sandy 1 1 1
sats 0 0 1
nil 0 0 1
Posted by ojie on Dec 28th, 2009

i want now number of the day form date 2009-12-31 ?
anybody help me ?
Posted by Dadaso on Dec 23rd, 2009

first of all thanks a lot.saves a lot of time
Posted by Saravana on Dec 9th, 2009

Veselin Stoilov,

Thanks a lot. Your code helped me to get the days between two dates.
Posted by mugger on Nov 27th, 2009

Oops. Should be <?php
Posted by mugger on Nov 27th, 2009

I put your code examples between <php? and ?>, tried both print $days; and echo $days; , and see NO OUTPUT. Very puzzled.
Posted by Arunkumar P on Nov 16th, 2009

nice work guys.. i found the solution.. thanks to all..
Posted by Paul on Nov 7th, 2009

Hi all, i need to display some thing like:
"1 years, 7 months, and 25 days" as the diference of two date.
can anybody help???
TIA
Posted by Hrithik on Aug 6th, 2009

Thnx Friends this code works for me also im very happy do the best

thxn again
Posted by sumesh on Jul 24th, 2009

Thanx Alexandar,
this code works for me thanx very much
Posted by lcy on Jul 22nd, 2009

Hi! Can i ask for some help here....
I've tested the following script and it works.
However, when i use date in d/m/y format instead of m/d/y, thing goes wrong and the correct number of days can't be generated.
What can i do to get it right?
Need this in urgent...really hope to get some assistance here...Thanks in advance!



function dateDiff($startDate, $endDate)
{
// Parse dates for conversion
$startArry = date_parse($startDate);
$endArry = date_parse($endDate);

// Convert dates to Julian Days
$start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);
$end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);

// Return difference
return round(($end_date - $start_date), 0);
}
Posted by sramesh on Jul 7th, 2009

I was reading some tutorial to find no. of days between days and could not find it. this saved me a lot of time...
Posted by ashwani on Jun 26th, 2009

first date diff tech.. is good .It works proper
Posted by Peter on Jun 20th, 2009

Hi all, a need to the dates between two other dates, based on a var $jump, like the example:

$jump = 2; // Important
$date1 = "2009-04-10";
$date2 = "2010-04-10";

So as a result i need to have the dates:
2009-04-10
2009-06-10
2009-08-10
2009-10-10
2009-12-10
2010-02-10
2010-04-10

Note that if i change the var $jump to 3 dates should be :
2009-04-10
2009-07-10
2009-10-10
2010-01-10
2010-04-10

How can i do this?
Thanks for help guys...
Posted by Tristavius on Jun 11th, 2009

Using your example of 20th May 2009 + 2 months...

$d = date('Y-m-d', mktime(0,0,0,5+2,20,2009));

The mktime function is clever enough to understand what happens if, for example, you were to add 2 months to November 2000.... it will be clever enough to understand that 11+2 = 13 which it knows isn't a month and of course knows it should be january. In this example it will also be clever enough to add 1 to the years, giving a return date of January 2001. Same goes for if you were to add 7 days to May the 31st, it will correctly return June 7th.

The mktime function also accepts variables and other functions, I will use 2 examples below to demonstrate...


// Using Variables
$day = 20;
$month = 5;
$year = 2009;
$d = date('Y-m-d', mktime(0,0,0,$month+2,$day,$year));

// Using Date Function To Add 2 To Current Date
$d = date('Y-m-d', mktime(0,0,0,date('m')+2,date('d'),date('Y')));


Finally, the 'Y-m-d' part can be changed to output a large number of configurations. The above returns [Full 4 Digit Year]-[Month With Leading Zero]-[Day With Leading Zero]. For a full list please see http://uk3.php.net/manual/en/function.date.php

Hope this helps!
Posted by Selom on May 20th, 2009

Very cool but how to get a date after adding 2 months to the current date? suppose i want to add 2 months to the $current_date ="Wednesday May 20, 2009". Thanks in advance
Posted by Lobos on Feb 6th, 2009

I tried
$days = (strtotime("2005-11-20") - strtotime("2005-11-26")) / (60 * 60 * 24);

and I get a number that is not a whole number, it has several decimal places, how can i fix that?

use round
round(strtotime("2005-11-26")) / (60 * 60 * 24));
Posted by Brian on Jan 24th, 2009

I tried
$days = (strtotime("2005-11-20") - strtotime("2005-11-26")) / (60 * 60 * 24);

and I get a number that is not a whole number, it has several decimal places, how can i fix that?

Thanks in advance.. :)
Posted by chinmya on Jan 13th, 2009

i want to find . number of saterday in the given range of dates ?
Posted by B on Nov 7th, 2008

X:

Thanks so much! The other solutions here don't seem to work for dates like 1901-01-01 2008-01-01.
Posted by Fredsfish on Aug 27th, 2008

Alexandar / Veselin has the example backwards.

$days_proc_time = (strtotime(date("Y-m-d")) - strtotime("2005-11-20") ) / (60 * 60 * 24);

Posted by X on Aug 6th, 2008

Try this... take not though, date_parse requires PHP version 5.1.3 or higher.


/**
* Finds the difference in days between two calendar dates.
*
* @param Date $startDate
* @param Date $endDate
* @return Int
*/
function dateDiff($startDate, $endDate)
{
// Parse dates for conversion
$startArry = date_parse($startDate);
$endArry = date_parse($endDate);

// Convert dates to Julian Days
$start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);
$end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);

// Return difference
return round(($end_date - $start_date), 0);
}
Posted by DOBRi on Apr 13th, 2008

Thanks a lot!!! I spent so time looking for this 2 lines!
Posted by Veselin Stoilov on Feb 23rd, 2008

if you want to see all dates between today and the next 20 days you need to use something like this


$startDate = date("Y-m-d", mktime(0, 0, 0, 2, 23, 2008));
$endDate = date("Y-m-d", mktime(0, 0, 0, 2, 23+20, 2008));
$i=0;
while ($temp <> $endDate) {
echo $temp = date("Y-m-d", mktime(0, 0, 0, 2, 23+$i, 2008));
$i++;
};
Posted by nel on Feb 23rd, 2008

ok...
how to list all date between this 2 date ?
Posted by Kumar on Dec 15th, 2007

Its a great solution, Thanks a lot.
Posted by JT on Sep 26th, 2007

Thank you very much
Posted by miko on Jul 10th, 2007

Try:


$days = (strtotime("2007-07-10") - strtotime("2007-01-01")) / (60 * 60 * 24);


It is not working properly!
Posted by craig on Jan 20th, 2007

arse error: syntax error, unexpected ';' in /home/craig/public_html/page/billing.php on line 1

this doesnt work .
Posted by Veselin on Jul 14th, 2006

what Alexandar showed is number of days between a date and the current date. It is probably obvious that you can use


$days = (strtotime("2005-11-20") - strtotime("2005-11-26")) / (60 * 60 * 24);
print $days;


for number of days between two dates.
Posted by Alexandar on Jun 15th, 2006

this is the easiest wa to calculate number of days:


$days = (strtotime("2005-11-20") - strtotime(date("Y-m-d"))) / (60 * 60 * 24);
print $days;
 
POST A REPLY
Your name:
Your email: (email address will not be posted on the website)
Reply:
Verification code:
type characters on the image in the text box