Search code examples
phpdatetimeforeachdecrement

php: how to decrement date time into a foreach for every item...?


... and not one time only...

    $now = date("D, d M Y H:i:s O");
    foreach ($items AS $item)
        {

            $currentDate = strtotime($now);
            $pastDate = $currentDate-(60*5);
            $formatDate = date("Y-m-d H:i:s", $pastDate);
            echo "\t\t<item>\n";
            echo "\t\t\t<pubDate>" . $formatDate . "</pubDate>\n" ;

now, i want formatDate decrementing of 5 minutes every time, i don't want the same time for every element... thanks


Solution

  • $currentDate = time();
    foreach ($items as $item){
        $formatDate = date("Y-m-d H:i:s", $currentDate);
        echo "\t\t<item>\n";
        echo "\t\t\t<pubDate>" . $formatDate . "</pubDate>\n" ;
        $currentDate = strtotime('-5 minutes', $currentDate);
    }