Jump to content

Complicated Date/Job Frequency calculation problem.


moonman89

Recommended Posts

Hey guys,

Hoping someone can help me with the logic for this one. I can't figure out how to do it.

I've got a table of jobs, with a field for start date (Or once off date) and a field for job frequency, IE. 14 days, 28 days 42 days ETC...

 

I need to select all jobs for a particular date EG, 1/7/09 ( I choose that month cause it has the same amount of days as the next )

 

If there is a job on 1/6/09 with a freq of 31 days, it needs to appear on my list.

But on the same note, if there is a job on 30/6/09 with a freq of 2 days, that needs to appear too.

Same as if there is a job on 12/06/09 with a freq of 19 days.

 

How would you go about selecting these jobs?

There has to be a way to do this...

 

The database is MySql.

Link to comment
Share on other sites

If I understood you correctly...

 

// example array of data. build from db query
$jobs[] = array('startDate' => "2009-01-01", 'frequency' => 2);
$jobs[] = array('startDate' => "2009-03-25", 'frequency' => 30);
$jobs[] = array('startDate' => "2009-06-14", 'frequency' => 12);
$jobs[] = array('startDate' => "2009-07-14", 'frequency' => 14);
$jobs[] = array('startDate' => "2009-07-25", 'frequency' => 3);

// target date
$tdate = "2009-07-28";
$date = strtotime($tdate);

// foreach job...
foreach($jobs as $job) {
  // get the timestamp for startDate + frequency days in future
  $jsD = strtotime($job['startDate']);
  $cDate  = mktime(0, 0, 0, date("m",$jsD),   date("d",$jsD)+$job['frequency'],   date("Y",$jsD));

  // if dates match up...
  if ($cDate == $date)
  // add to list
    $targetDates[] = $job['startDate'];
} // end foreach

// dump out data and results
echo "<pre>";
print_r($jobs);
echo "target date: " . $tdate . "<br/>"; 
print_r($targetDates); 

 

output:

Array
(
    [0] => Array
        (
            [startDate] => 2009-01-01
            [frequency] => 2
        )

    [1] => Array
        (
            [startDate] => 2009-03-25
            [frequency] => 30
        )

    [2] => Array
        (
            [startDate] => 2009-06-14
            [frequency] => 12
        )

    [3] => Array
        (
            [startDate] => 2009-07-14
            [frequency] => 14
        )

    [4] => Array
        (
            [startDate] => 2009-07-25
            [frequency] => 3
        )

)
target date: 2009-07-28
Array
(
    [0] => 2009-07-14
    [1] => 2009-07-25
)

Link to comment
Share on other sites

If I understood you correctly...

 

// example array of data. build from db query
$jobs[] = array('startDate' => "2009-01-01", 'frequency' => 2);
$jobs[] = array('startDate' => "2009-03-25", 'frequency' => 30);
$jobs[] = array('startDate' => "2009-06-14", 'frequency' => 12);
$jobs[] = array('startDate' => "2009-07-14", 'frequency' => 14);
$jobs[] = array('startDate' => "2009-07-25", 'frequency' => 3);

// target date
$tdate = "2009-07-28";
$date = strtotime($tdate);

// foreach job...
foreach($jobs as $job) {
  // get the timestamp for startDate + frequency days in future
  $jsD = strtotime($job['startDate']);
  $cDate  = mktime(0, 0, 0, date("m",$jsD),   date("d",$jsD)+$job['frequency'],   date("Y",$jsD));

  // if dates match up...
  if ($cDate == $date)
  // add to list
    $targetDates[] = $job['startDate'];
} // end foreach

// dump out data and results
echo "<pre>";
print_r($jobs);
echo "target date: " . $tdate . "<br/>"; 
print_r($targetDates); 

 

output:

Array
(
    [0] => Array
        (
            [startDate] => 2009-01-01
            [frequency] => 2
        )

    [1] => Array
        (
            [startDate] => 2009-03-25
            [frequency] => 30
        )

    [2] => Array
        (
            [startDate] => 2009-06-14
            [frequency] => 12
        )

    [3] => Array
        (
            [startDate] => 2009-07-14
            [frequency] => 14
        )

    [4] => Array
        (
            [startDate] => 2009-07-25
            [frequency] => 3
        )

)
target date: 2009-07-28
Array
(
    [0] => 2009-07-14
    [1] => 2009-07-25
)

 

 

OK, that has done 90% of what I want, but I think that is only catching the first frequency I believe -- correct me if I'm wrong??

I need to add another loop for each job that keeps going until start date + frequency is past the requested date.

 

Does that sound right?

I need to catch EVERY occurrence, weather it the first or the 1111th.

 

 

Link to comment
Share on other sites

I understood your OP to mean you have a target date, and you have a list of all these other dates with a number "frequency" and you wanted to know which date(s)+frequency = target date.

 

I'm not really sure what you're asking in this last post though.

 

What is 'OP'? Everyone is using it and I have no idea about what you all are talking about. Did I miss a memo?

Link to comment
Share on other sites

moonman:

 

I'm kind of sort of getting a vague idea maybe you are trying to figure out a "next available date" sort of thing, based off of a start date, frequency and target date.  So for instance, if you have the following info:

 

  start date    frequency

A 2009-06-01  30

B 2009-06-15  10

C 2009-07-09  2

 

target date

2009-07-28

 

You are wanting...

 

Next available date:

A 2009-07-31

B 2009-08-04

C 2009-07-31

 

is that correct?

 

 

Link to comment
Share on other sites

I'm kind of sort of getting a vague idea maybe you are trying to figure out a "next available date" sort of thing, based off of a start date, frequency and target date.

 

I think he is trying to build an advertising kind of thing for jobs. How higher the frequency (and the money) the more it is shown, would be my best bet

Link to comment
Share on other sites

  • 3 weeks later...

I ended up finding a much simpler way of doing it in mysql.

 

To answer your question though.

It is for a mowing franchise, but the person who wrote the program that handles all the job scheduling did the tables roughly like so:

 

Schedule_Date: 28-1-2009

Freq: 14

 

So what I'm after is a selection of all jobs for say 28-1-2009, I should get any jobs listed on the 28th that don't have a freq. And I should also get any jobs listed on either the 14th, or the December 31st that have a freq of 14 back until schedule_date or the job is cancelled.

 

Link to comment
Share on other sites

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.