days between two dates

Sparky
June 15, 2006, 4:13 am
days between two dates
hi, can you please tell me how to calculate number of days between two days using php. Thanks
Alexandar
June 15, 2006, 5:43 am
Re:
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;
Veselin
July 14, 2006, 10:55 am
Re:
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.
craig
January 20, 2007, 2:49 pm
Re:
arse error: syntax error, unexpected ';' in /home/craig/public_html/page/billing.php on line 1

this doesnt work .
miko
July 10, 2007, 9:00 pm
Re:
Try:

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

It is not working properly!
JT
September 26, 2007, 8:11 pm
Re: days between two dates
Thank you very much
Kumar
December 15, 2007, 8:38 am
Re: days between two dates
Its a great solution, Thanks a lot.
nel
February 23, 2008, 11:54 am
Re:
ok...
how to list all date between this 2 date ?
Veselin Stoilov
February 23, 2008, 7:38 pm
Re:
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++;
};
DOBRi
April 13, 2008, 10:38 pm
Re:
Thanks a lot!!! I spent so time looking for this 2 lines!
X
August 6, 2008, 9:15 pm
Re:
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);
}
Fredsfish
August 27, 2008, 7:27 pm
Re:
Alexandar / Veselin has the example backwards.

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

B
November 7, 2008, 11:24 pm
Re:
X:

Thanks so much! The other solutions here don't seem to work for dates like 1901-01-01 2008-01-01.
chinmya
January 13, 2009, 9:23 am
Re:
i want to find . number of saterday in the given range of dates ?
Brian
January 24, 2009, 6:06 am
Re: Dates - WHOLE NUMBERS
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.. :)
Lobos
February 6, 2009, 8:36 pm
Re:
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));
Selom
May 20, 2009, 1:38 pm
Re:
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
Tristavius
June 11, 2009, 4:39 am
Re: Selom
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!
Peter
June 20, 2009, 11:28 pm
Re:
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...
ashwani
June 26, 2009, 11:02 am
Re: date diff
first date diff tech.. is good .It works proper
sramesh
July 7, 2009, 8:38 pm
Re:
I was reading some tutorial to find no. of days between days and could not find it. this saved me a lot of time...
lcy
July 22, 2009, 11:51 am
Re: Why it doesn't work when using "d/m/y" format??
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);
}



sumesh
July 24, 2009, 2:42 pm
Re: thanx
Thanx Alexandar,
this code works for me thanx very much
Hrithik
August 6, 2009, 12:37 pm
Re: thanx
Thnx Friends this code works for me also im very happy do the best

thxn again
Paul
November 7, 2009, 9:34 pm
date stuff
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
Arunkumar P
November 16, 2009, 4:32 pm
Re:
nice work guys.. i found the solution.. thanks to all..
mugger
November 27, 2009, 2:08 am
Re: date diffs
I put your code examples between <php? and ?>, tried both print $days; and echo $days; , and see NO OUTPUT. Very puzzled.
mugger
November 27, 2009, 2:13 am
Re:
Oops. Should be <?php
Saravana
December 9, 2009, 1:31 pm
Re:
Veselin Stoilov,

Thanks a lot. Your code helped me to get the days between two dates.
Dadaso
December 23, 2009, 12:56 pm
Re: Saves a lot of time
first of all thanks a lot.saves a lot of time
ojie
December 28, 2009, 11:20 am
Re:
i want now number of the day form date 2009-12-31 ?
anybody help me ?
adi
January 22, 2010, 2:52 pm
i want to print

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
Jackson
March 5, 2010, 9:55 pm
Re: This will return days as array
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."
";
}
siti
March 25, 2010, 9:04 am
Re: can you fix it for me?
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);
AJay Bairagi
June 11, 2010, 4:10 pm
add two dates and months
I want to add two dates and add month.
Ranga Pathmasiri
June 23, 2010, 5:05 pm
Re:
This is a great job Jackson . Thanks Jackson
muhammad
August 19, 2010, 9:59 am
Re:
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
Reply
Title:
Your name:
Your email: (email address will not be posted on the web site)
Reply:
Verification