Olly Vitts
Olly Vitts

Reputation: 99

generate datetime with loop for or foreach

is possible generate datatime with loop for or foreach with 30 minutes intervals?

for example:

for($i=0;$i<?;$i ??){
echo ;
}

now is 2011-12-18 02:24:00

and i would like receive:

2011-12-18 02:00:00
2011-12-18 02:54:00
2011-12-18 03:24:00
2011-12-18 03:54:00
2011-12-18 04:24:00
2011-12-18 04:54:00
2011-12-18 05:24:00

Upvotes: 1

Views: 1549

Answers (3)

PeeHaa
PeeHaa

Reputation: 72652

You can use PHP's DateTime for this:

$datetime = new DateTime();

echo $datetime->format('Y-m-d H:i:s'), "\n";

for($i=0;$i<10;$i++){
    $datetime->modify('+30 minutes');

    echo $datetime->format('Y-m-d H:i:s'), "\n";
}

Demo

Upvotes: 1

devasia2112
devasia2112

Reputation: 6014

$sStartDate = date('Y-m-d H:i:s');
for ($i=0;$i<10;$i++)
{
  echo '<br />'.$sStartDate  = date('Y-m-d H:i:s', strtotime('+30 minutes', strtotime($sStartDate)));
}

Upvotes: 0

middus
middus

Reputation: 9121

This can easily be implemented using php's time() function and adding 30*60 seconds.

$time = time();
for($i=0; $i<10; $i++){
  // add 30 minutes à 60 seconds
  $time += 30*60;
  echo date('Y-m-d H:i:s', $time), "\n";
}

See a demo on codepad

Upvotes: 0

Related Questions