How to add +10 whole seconds to date in a loop in PHP

Asked

Viewed 739 times

0

I need the loop be executed 10 times, and that each loop the variable $tempo that has the date, hour, minute and seconds receive +10 seconds, but that in the final result is not displayed in fragmented seconds, ex 08, 15, 59, wish only integers, or more specifically 00, 10, 20, 30, 40, 50.

The result of the variable $tempo in the loop should be something like:

2001_12_15_001510
2001_12_15_001520
2001_12_15_001530
2001_12_15_001540
2001_12_15_001550
2001_12_15_001600
2001_12_15_001610
2001_12_15_001620
2001_12_15_001630
2001_12_15_001640

I could only get the seconds in integer numbers without the loop with

if(in_array($segundos, range("00", "09"))){
    $segundos = "00";
}
  • On which operating system? A short answer depends on the strptime() to convert the date into the format 2001-12-15 00:15:19, but this function is not implemented in Windows.

  • I’m running php on windows

2 answers

1


I don’t understand exactly what you’re trying to do. But this code will list exactly what you want. Let me know if it’s not what you need. Please publish some excerpt of your code so I know better what you need.

$time = strtotime(str_replace('_', '', '2001_12_15_001510'));
for($i = 0; $i < 10; $i++){
    echo date('Y_m_d_His', $time).' - '.$time.'<br>';
    $time += 10;
}

The way out is like this

2001_12_15_001510 - 1008371710
2001_12_15_001520 - 1008371720
2001_12_15_001530 - 1008371730
2001_12_15_001540 - 1008371740
2001_12_15_001550 - 1008371750
2001_12_15_001600 - 1008371760
2001_12_15_001610 - 1008371770
2001_12_15_001620 - 1008371780
2001_12_15_001630 - 1008371790
2001_12_15_001640 - 1008371800

As you can see, you have only seconds or date with the format you requested.

  • It worked perfectly, thanks for the speed, I was able to implement and test.

1

The numbers come out broken because the input has a value that is not multiple of ten. To fix this, you need to advance to the nearest time whose seconds is multiple of 10.

We do it with the operator mod (in PHP, %), He takes the rest of a division. Suppose the seconds are 32, we need to get to 40 to start adding 10 out of 10. You divide 32 by 10, and you take the rest of the division, which is 2. The difference between 10 and 2 (10 - 2 = 8) is what we need to add to get to 40 (32 + 8 = 40). The full expression is 32 + (10 - (32 % 10)) = 40

See the implementation:

// Define a zona antes de mexer com DateTime
date_default_timezone_set('America/Sao_Paulo');

// A data inicial tem que estar nesse formato (ISO 8601)
$inicial = '2012-07-08 11:14:58';

$tempo = new DateTime($inicial);

// Encontra o horário mais próximo com os segundos múltiplo de 10
$addSegundos = 10 - ($tempo->format('s') % 10);
$tempo->add(new DateInterval('PT'.$addSegundos.'S'));

for ($i = 0; $i < 10; $i++) {
    if ($i != 0)
        // Adiciona 10 segundos ao $tempo
        $tempo->add(new DateInterval('PT10S'));

    echo $tempo->format("Y_m_d_His\n");
}
  • Well explained, but I preferred the other because it is simpler to implement.

  • 1

    Relax, I figured you needed to deal with entrances where seconds are broken. If the seconds are already multiples of 10, the other answer is more appropriate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.