Posted by raulgonzalez, 08-31-2008, 01:48 PM | Hello,
I have a problem. I was given the task of writing a script that would function only if the employee had been working for at least 6 months.
I use the strtotime() function to calculate the days between dates and work with it based on a 180 day period. Of course not all months are based on a 30 day period so the calculation might return incorrect by a few days in some cases.
Does anyone know of a function that can return the number of months between dates percicely?
Thank you.
|
Posted by Burhan, 08-31-2008, 05:10 PM | Since employees are not paid in calendar days but rather working days, you need to calculate it based on the number of working days in a month.
|
Posted by raulgonzalez, 08-31-2008, 11:08 PM | No, we are paid a yearly salary no matter if there is a holiday or not. The Human resources considers an empoyee being employed even on saturdays and sundays. We are always on call too.
I just need to know if there is someone with a function that does that before I embark into writing it from scratch
|
Posted by foobic, 08-31-2008, 11:44 PM | I think calculating a precise interval in months is the wrong way to go about it. Instead, I would add a 6 month interval to the start date and compare the result to the current / end date.
The date_add function is in the PHP manual but looks like it's not yet in release versions. If it's a database app the MySQL function exists.
|
Posted by raulgonzalez, 09-01-2008, 01:43 PM | I found this on the MYSQL manual and tried it using the mysql command line.
SELECT PERIOD_DIFF( EXTRACT( YEAR_MONTH FROM date1 ),
EXTRACT( YEAR_MONTH FROM date2 ) );
it works, but how do I use it in a php script so that I can output the results to the browser?
I've never used mysql as means of calculations.
Thank you.
|
Posted by raulgonzalez, 09-01-2008, 02:04 PM | Well, this does output the results to the browser, but doesn't calculate it as I expected it. It outputs "6" and I was expecting a 5 as the month gets completed really on the 10th of July.
Hopefully someone else can find it useful.
|
Posted by Sheps, 09-01-2008, 03:36 PM | PHP I assume?
Use the strtotime(). Then, take that and put it into a date() function which just extracts the numeric month, then take the date and make sure it is less then or equal to the current date.
When I get home, I can write up some quick code for you.
|
Posted by foobic, 09-01-2008, 06:47 PM | If you use periods you're completely discarding the day values that give you partial months - it won't do what you want. You may as well just calculate (year * 12 + month) and compare. This is what I meant:
or for test purposes:
Edit: This is a more interesting one (it returns true):
Last edited by foobic; 09-01-2008 at 06:57 PM.
|
Posted by raulgonzalez, 09-01-2008, 09:46 PM | Foobic,
That's exactly what I wanted. I tried different examples, leap yers, months that end in different days. and it always worked.
//this returns true because 28 of february is the end of the month so mission accomplished
$query = "SELECT DATE_ADD('2008-08-30', INTERVAL 6 MONTH) <= '2009-02-28'";
//this returns false because 28 of february is NOT the end of the month so one more day to go before completing 6 months
$query = "SELECT DATE_ADD('2007-08-30', INTERVAL 6 MONTH) <= '2008-02-28'";
Thank you very much for your contribution. I hope someone finds it useful as I got tired of researching the web without success.
|
|
Add to Favourites
Print this Article |