Jump to content

Recommended Posts

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

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.

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
)

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.