count the days between 2 date fields.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

count the days between 2 date fields.

Post by jauson »

Hi, I am looking all over the internet for a days but Im getting a hard time to find the best tutorial for counting the days for 2 different date fields. I was looking for live counting, I mean for example. I have 3 fields.

datefrom: 1/12/2011
dateto: 1/14/2011
total days: 2 days (readonly)

when the user set the date_from and date_to, total_days field will automatically count when 2 fields are set. is it possible in php? any suggestion or link will be appreciated. :) thanks guys
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: count the days between 2 date fields.

Post by Celauran »

Code: Select all

$date1 = strtotime($date1);
$date2 = strtotime($date2);

$diff = ($date2 - $date1) / (24 * 60 * 60);

echo $diff;
dsnraju
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2011 3:39 am

Re: count the days between 2 date fields.

Post by dsnraju »

Code: Select all

<?php

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

?>
Last edited by Benjamin on Sun Oct 30, 2011 4:12 am, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: count the days between 2 date fields.

Post by Gopesh »

Hi,U can do this dynamically with php and ajax.It can also be done with javascript Ex:

Code: Select all

<?php
<form>
 <input type="text" name="textFromDate" id="textFromDate" />
 <input type="text" name="textToDate" id="textToDate"  onchange="getdate()"/>
 <input type="text" name="textTotalDays" id="textTotalDays" readonly="readonly" />
 <input type="submit" name="buttonApply" id="buttonApply" value="Apply Leave" />
</form>
?>
Function getdate() is :

Code: Select all

function getdate()
{
var one_day=1000*60*60*24;
var fdate = document.getElementById("textFromDate").value;
var tdate = document.getElementById("textToDate").value;

var date1=new Date(fdate);
var date2=new Date(tdate);
if(date1>date2)
{
alert("Not possible");
}
else{




//Calculate difference between the two dates, and convert to days


var Diff=Math.ceil((date2.getTime()-date1.getTime())/one_day);

document.getElementById("textTotalDays").value=Diff+1;
}
} 
Hope it will be useful to u..
[Sorry for posting javascript code here]
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: count the days between 2 date fields.

Post by jauson »

Hi thanks Gopesh,

I found that the total_days for dateFrom and dateTo is wrong. the value is advance of 1 day always/
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: count the days between 2 date fields.

Post by Gopesh »

Have u run that code? what is the result ??. if the result is wrong,rework on that piece of code.i just give u an example for dynamically populating values to the textbox .
jauson
Forum Contributor
Posts: 111
Joined: Wed Oct 05, 2011 12:59 am

Re: count the days between 2 date fields.

Post by jauson »

thank you Gopesh for a wonderful tutorial. I just run the code and it works perpectly. sorry for my previous comment.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: count the days between 2 date fields.

Post by pickle »

Be sure to floor() the result, as twice a year the number won't be exact due to Daylight Saving Time shifts.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply