1

I want to do a schedule pattern that repeat the value every 4 days... Below the php script is looping the date from 1-Sept til 30-Sept.I want to repeat the value for every 4 days... For example, my value is (A,B,C,D) and I want to repeat it every 4day...thats mean

1/9 = A 
2/9 = B 
3/9 = C 
4/9 = D 
5/9 = A 
6/9 = B 
7/9 = C 
8/9 = D 

...... til 30/9 then stop...

<?php  
// Set timezone
date_default_timezone_set('UTC');

// Start date
$start_date = '2015-09-01';
// End date
$end_date = '2015-09-30';

$value = "A";

while (strtotime($start_date) <= strtotime($end_date)) {
echo "$start_date <br>";
$start_date = date ("Y-m-d", strtotime("+1 day", strtotime($start_date)));

}
 ?>

3 Answers 3

1

What about something like this:

$values = ['A', 'B', 'C', 'D'];

// Start date
$start_date = '2015-09-01';
// End date
$end_date = '2015-09-30';

$current_date = $start_date;
$index = 0;
while (strtotime($current_date) <= strtotime($end_date)) {
    $i = $index % count($values);
    echo "$current_date: $values[$i]\n";
    $current_date = date("Y-m-d", strtotime("+1 day", strtotime($current_date)));
    $index++;
}

% is a modulus operator, which returns remainder of a division. $a % $b returns remainder of $a divided by $b. So for example 6 % 4 is 2, 7 % 4 is is 3 and so on.

Sign up to request clarification or add additional context in comments.

4 Comments

You're welcome, I'm glad it helped you. You can also up-vote my answer as a reward ;)
count($values) can be pulled out of the loop.
You can avoid three strtotime date string to UNIX time conversions per iteration.
@mvw Put it as a comment of the question, please. It's his code, not mine. I didn't change his code so he can easily recognize, what's solution to his question. But I agree with your comments.
1

And another version:

<?php  
// Set timezone
date_default_timezone_set('UTC');

// Start date
$start_date = strtotime('2015-09-01');
// End date
$end_date = strtotime('2015-09-30');

$value = "A";

for ($date = $start_date; $date <= $end_date; 
     $date = strtotime("+1 day", $date)) {
  $date_str = date("Y-m-d", $date);
  echo "$date_str: $value<br>\n";
  $value++;
  if ($value > "D") {
    $value = "A";
  }
}
?>

Note: While the variables have date suffixes, they contain UNIX time values, and not some kind of date object.

3 Comments

This will work only in this specific case, but not for example for these values: $values = ['first', 'second', 'third', 'fourth'].
It would also work for 42,43,44,45 and all types that are ordered, have addition defined, and when the values succeed each other.
Yep, but that's what I'm talking about - it will work for specific input values only, it's not general algorithm. So it would be good to make a note about that in your answer. I'm not saying it's bad, I'm saying that it will work just for a very specific cases.
0
<?php
// Set timezone
date_default_timezone_set('UTC');

// Start date
$start_date = '2015-09-01';
// End date
$end_date   = '2015-09-30';

$value = array(
    "A",
    "B",
    "C",
    "D"
);
$i     = 0;
while (strtotime($start_date) <= strtotime($end_date)) {
    echo "$start_date = " . $value[$i] . " <br>";
    if ($i == count($value) - 1) {
        $i = 0;
    } else {
        $i++;
    }
    $start_date = date("Y-m-d", strtotime("+1 day", strtotime($start_date)));

}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.