Off the top of my head I don't know of anything that is already written to do this, so I leave it to others to speak there. I would say that it's just as easy to write a small function to calculate the middle for you and then build your date from that.
So, are you considering half days or only whole days? If you're taking into consideration half of a day then all of the below is not even a necessary consideration and you can just take the last day in the month and divide it by 2 to get your middle point.
If you're not doing half days, then I would ask what are you considering the middle of the month?
31 / 2 = 15.5
30 / 2 = 15
29 / 2 = 14.5
28 / 2 = 14
For the month that has 31 days in it what are you considering the middle? You have two scenarios. One with 1-15 (15 days) and 16-31 (16 days), and the second 1-16 (16 days) and 17-31 (15 days.) Same thing for a month with 29 days in it except it would be 1-14 and 15-29 or 1-15 and 16-29.
All of that is to get down to the case that if you're using the first of the two scenarios above, that means your middle of the month only toggles between the 14th and 15th of the month. So you could have the SQL query do it or you could have PHP do it. Personally I'd go the route of having the PHP calc it and then just had a straight query to the database.
If you're the first scenario then the function would look like this.
function middleOfMonth( $date = NULL ){
return floor( date( 't', isset( $date ) ? strtotime( $date ) : strtotime("now") ) / 2 );
}
If you're the second scenario, it's only a little different using ceiling instead of floor.
function middleOfMonth( $date = NULL ){
return ceiling( date( 't', isset( $date ) ? strtotime( $date ) : strtotime("now") ) / 2 );
}
Hopefully I've not gone off in an overly complicated consideration of this, but it's how I would approach it. Maybe someone else will have an even better answer and teach me something.
Thanks,
Daniel H.
Google Voice:
(901) 214-5326
- Copying one is plagiarism, copying many is research.