Page 1 of 1
Help with Dates
Posted: Sun Aug 26, 2012 2:08 pm
by Live24x7
need help
I have two date fields in my form - $startdate and $enddate.
the dates are entered in format MM-DD-YYYY
I need to calculate the number of days between the two dates but with a slight twist.
Normally number of days could be calculated using :
Code: Select all
$timediff = (strtotime($startdate) - strtotime($enddate))/(60 * 60 * 24);
But in this case i need to take:
1) the actual number of days for the start month
+
2) 30 days for all in-between months - even where months in the period may have 28, 29 or 31 days
+
3) Actual number of days for the end month.
For example:
If startdate = 7 Dec 2012
and enddate = 18 Aug 2013
i will take 24 days in dec
+
30*7 for inbetween 7 months (January 2013 to July 2013)
+
17 days in aug
How can i do this ?
Thanks for answering
Re: Help with Dates
Posted: Sun Aug 26, 2012 5:48 pm
by requinix
You've basically answered your own question... I can't imagine you're that far from having working code. What do you have now?
Re: Help with Dates
Posted: Sun Aug 26, 2012 5:53 pm
by Live24x7
Hi requinix -
how come i have answered my own question
sorry i did not get it
i am yet to write a piece of code regarding this as i am yet to figure out where to begin
Re: Help with Dates
Posted: Sun Aug 26, 2012 7:10 pm
by requinix
You said exactly what you need to do:
Live24x7 wrote:If startdate = 7 Dec 2012
and enddate = 18 Aug 2013
i will take 24 days in dec
+
30*7 for inbetween 7 months (January 2013 to July 2013)
+
17 days in aug
- December has 31 days. 31-7=24 so there's the starting point.
- Having closed out December 2012 you now start counting from January 2013. To August that's 7 months and 30*7=210 days.
- Then the 18 days in August.
And the total is 24+210+18=252 days.
What are the values of the start date and end date variables you have? Like are they strings, or Y-M-Ds, or Unix timestamps?
Re: Help with Dates
Posted: Mon Aug 27, 2012 3:20 am
by Live24x7
My startdate and enddate variables are user input string in the form of MM/DD/YYYY.
thanks a lot.
Re: Help with Dates
Posted: Mon Aug 27, 2012 5:01 am
by requinix
Break them apart into the individual components: month, day, and year. From there you can use mktime() and then date() to get the number of days in a month (or you can do the logic yourself, which is easy so maybe that'd be better). So with that said, try writing some code and see how far you can get.
Re: Help with Dates
Posted: Mon Aug 27, 2012 12:15 pm
by Live24x7
Hi Requinix -
I have tried this piece of code and all works well except that:
I am not getting the right number of intermittent months between two dates (excluding the start and end month).(see the code between commented area - which often gives the wrong number of months).
So if my dates are 5th Nov, 2012 and 9th august, 2013 - i want the number of months excluding month Nov-Year12 and month Aug-Year13. i;e 8 months.
If i get this figure ($nummonthsbetweentwodates) correct - the code below should give what i want.
Code: Select all
$sd = "03/07/2010";
$ed = "08/13/2010";
$sdparts = explode("/", $sd);
$edparts = explode("/", $ed);
$totnumdaysinmonth = cal_days_in_month(CAL_GREGORIAN, $sdparts[0] , $sdparts[2]);
/* calculate num of months between two dates - this is where i am stuck*/
$date1 = new DateTime("$sd");
$date2 = new DateTime("$ed");
$interval = date_diff($date1, $date2);
$nummonthsbetweentwodates = $interval->format('%m'); // this is not correct - i know why it is wrong
//but i could not find the appropriate function to handle this.
/*eof num of months calculation*/
$totnumdays = ($totnumdaysinmonth-$sdparts[1]-1)+ 30*$nummonthsbetweentwodates + ($edparts[1]-1);
echo $totnumdays ;
thanks a lot
Re: Help with Dates[resolved]
Posted: Mon Aug 27, 2012 1:45 pm
by Live24x7
hi requinix - got this working

thanks for all your help.. feels good
