work out months from dates inputted

A place for users and developers of the Xataface to discuss and receive support.

work out months from dates inputted

Postby cantlep » Mon May 17, 2010 2:45 am

Hi All,

here's my second question for the day.

If I use the date function to select a "start date" and an "end date" for a "new entry" in one of my tables, I'd like something that auto calculates how many months that is in one of my columns. Can anyone help at all?

Cheers

Paul
cantlep
 
Posts: 172
Joined: Fri Mar 05, 2010 2:14 am

Re: work out months from dates inputted

Postby shannah » Mon May 17, 2010 8:16 am

You might consider using a grafted field to have mysql make the calculation for you.
Code: Select all
__sql__ = "select t.*, datediff(t.end_date,t.start_date) as num_days from mytable t"


This would create a column called num_days. You could estimate number of months from this.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: work out months from dates inputted

Postby cantlep » Tue May 18, 2010 6:18 am

Hi Steve,

Excellent, many thanks. period_diff did the trick with the same code.

Thanks again.

Paul
cantlep
 
Posts: 172
Joined: Fri Mar 05, 2010 2:14 am

Re: work out months from dates inputted

Postby cantlep » Tue May 18, 2010 12:21 pm

I thought period_diff sorted out the months for me but didn't :-(

Is there anything like oracle's months_between() that you know of?

Cheers
cantlep
 
Posts: 172
Joined: Fri Mar 05, 2010 2:14 am

Re: work out months from dates inputted

Postby shannah » Tue May 18, 2010 12:49 pm

I was a little surprised when you said that you got period_diff to work for you. The mysql docs stated that the inputs for period_diff are not dates, but rather are strings of the form 'YYMMDD'.

I didn't see any specific function to give the month diff. My suggestion to do the data_diff would allow you to do a pretty easy calculation of month difference in PHP (e.g. in the fieldname__display() method).
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 39 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved