nickelus Posted July 15, 2009 Share Posted July 15, 2009 hi, first post here at freaks, thanks in advance. what i'm trying to do is get a range of dates that includes only the 1st 8th 15th and 22nd of each month for the whole the range. i'm not sure about posting code here yet so i will detail first what i've tried. I started with a function that gets the dates between the values works fine i decided to filter the specific dates using an array search and this is where im stuck any thoughts or ideas are welcome Quote Link to comment https://forums.phpfreaks.com/topic/165997-php-date-range-array-limit-to-certain-dates/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 15, 2009 Share Posted July 15, 2009 Do it in your query - WHERE DAYOFMONTH(your_date_column) IN(1,8,15,22) AND the_rest_of_your_where_clause_here Quote Link to comment https://forums.phpfreaks.com/topic/165997-php-date-range-array-limit-to-certain-dates/#findComment-875503 Share on other sites More sharing options...
phporcaffeine Posted July 15, 2009 Share Posted July 15, 2009 How are your date values stored? timestamp? Quote Link to comment https://forums.phpfreaks.com/topic/165997-php-date-range-array-limit-to-certain-dates/#findComment-875509 Share on other sites More sharing options...
nickelus Posted July 15, 2009 Author Share Posted July 15, 2009 Thanks for the insane fast responses, very awesome. To add some more detail. The start and end dates are posted through a form So I'm setting those as variables at the beginning of my action script, and building the array with those arguments. The dates 1, 8, 15 and 22 come from a variable based on whether weekly, monthly or biweekly. case one would contain all of the dates, case two would be one of the dates(to be chosen) , and case three would be two of the dates (two weeks apart) respectively. After that I'll add the same two columns (client_id & contract_id) to each row and insert those to db as a schedule. And that's pretty much the functionality. Quote Link to comment https://forums.phpfreaks.com/topic/165997-php-date-range-array-limit-to-certain-dates/#findComment-875535 Share on other sites More sharing options...
.josh Posted July 15, 2009 Share Posted July 15, 2009 function getDates($days,$sDate,$eDate,$format = "") { $days = (is_array($days))? $days : array($days); $sDate = strtotime($sDate); $eDate = strtotime($eDate); $dayRange = range($sDate,$eDate,60*60*24); foreach ($dayRange as $d) { if (in_array(date("j",$d),$days)) $daysFound[] = ($format != "")? date($format,$d) : $d; } // end foreach return $daysFound; } // end function getDates $days : Day(s) you want to search for within range. Accepts a single (int) or array of (int). $sDate : Start of date range. Accepts any day/month/year format recognized by strtotime $eDate : End of date range. Accepts any day/month/year format recognized by strtotime $format : (optional) Format of returned dates. See date for how to format. If no format is passed, the unix timestamps are returned. Example use: $days = array(1,8,15,22); $startDate = '2009-06-22'; $endDate = '2009-07-15'; $format = 'Y-m-d'; // passing format $daysFound = getDates($days,$startDate,$endDate,$format); echo "<pre>"; print_r($daysFound); // not passing format $daysFound = getDates($days,$startDate,$endDate); print_r($daysFound); echo "</pre>"; output: Array ( [0] => 2009-06-22 [1] => 2009-07-01 [2] => 2009-07-08 [3] => 2009-07-15 ) Array ( [0] => 1245646800 [1] => 1246424400 [2] => 1247029200 [3] => 1247634000 ) Quote Link to comment https://forums.phpfreaks.com/topic/165997-php-date-range-array-limit-to-certain-dates/#findComment-875551 Share on other sites More sharing options...
nickelus Posted July 15, 2009 Author Share Posted July 15, 2009 that's gotta be the coolest thing Ive ever seen. nice, simple, clean i try to plan out my stuff like this but I'm not there yet where i can do stuff like that (in seconds not to mention) thankyou for that knowledge and inspiration works awesome i assigned the $days to a switch that rotates the array based on selection monthly,weekly,biweekly, and voila i really have learned something today Quote Link to comment https://forums.phpfreaks.com/topic/165997-php-date-range-array-limit-to-certain-dates/#findComment-875573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.