1

Ok so I am trying to schedule a campaign with mailchimp API and so far everything works fine except for the last else if that doesn't put 00 in my variable. I know php will put only 1 zero in the array and can't think of a work around for this. Help ! The code below is used to take a date, put it in iso 8601 format and then I use substr_replace() to schedule the campaign to the next time lapse available for mailchimp (15, 30, 45, 00).

I tried to change it for a string "00" instead but mailchimp does not schedule it.

$date_temp = new DateTime($date_debut);
$date_debut_iso = $date_temp->format('c');

$test = explode(':', $date_debut_iso);

//campaign has to be scheduled on :00, :15, :30, :45
if($test[1] >= 0 && $test[1] <= 15){
    $test[1] = 15;
}else if($test[1] >= 16 && $test[1] <= 30){
    $test[1] = 30;
}else if($test[1] >= 31 && $test[1] <= 45){
    $test[1] = 45;
}else if($test[1] >= 46 && $test[1] <= 59){
    $test[1] = 00;
}

$new_date = substr_replace($date_debut_iso, $test[1], 14, 2);

i need the last else if to store 00 in my array $test[1] instead of 0. String doesn't work.

acuz3r
  • 43
  • 9

1 Answers1

1

My only guess as to why setting $test[1] = '00'; wouldn't have worked is that all the other cases increase the time (or leave it the same), while that case decreases it because you're setting the minute to zero without changing the hour.

You don't really need to do all the string manipulation though. You can just get the minutes from the DateTime object and do a little math, then output the final result with ->format().

$date_temp = new DateTime($date_debut);

if ($offset = $date_temp->format('i') % 15) {
    $date_temp->modify('+' . (15 - $offset) . ' minutes');
};

$new_date = $date_temp->format('c');

The calculation is basically: if the remainder of the minutes divided by 15 is non-zero, increase the time by 15 - remainder minutes. This will also increment the hour appropriately for the > 45 case.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • I think you are right, im setting the minute to 0 without changing the hour so mailchimp couldnt schedule it properly, thanks! – acuz3r Jul 29 '19 at 20:51