php:如何将每个项目的日期时间递减到 foreach 中...?

发布于 2024-11-25 22:21:00 字数 448 浏览 2 评论 0原文

...而且不仅仅是一次...

    $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" ;

现在,我希望 formatDate 每次递减 5 分钟,我不希望每个元素都有相同的时间...谢谢

... 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深府石板幽径 2024-12-02 22:21:00

你最初的减去(60*5)的想法是完全正确的;您只需要不断从工作值中减去它,而不是按照代码重复从原始值中减去它:

$looptime = time();
foreach ($items AS $item) {
    $looptime -= (60*5);
    $formatDate = date("Y-m-d H:i:s", $looptime);
    echo "\t\t<item>\n";
    echo "\t\t\t<pubDate>" . $formatDate . "</pubDate>\n" ;
}

Your original idea of subtracting (60*5) was exactly right; you just need to keep subtracting it from a working value, rather than repeatedly subtracting it from the original value as per your code:

$looptime = time();
foreach ($items AS $item) {
    $looptime -= (60*5);
    $formatDate = date("Y-m-d H:i:s", $looptime);
    echo "\t\t<item>\n";
    echo "\t\t\t<pubDate>" . $formatDate . "</pubDate>\n" ;
}
帥小哥 2024-12-02 22:21:00
$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);
}
$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);
}
静待花开 2024-12-02 22:21:00

如果您使用 PHP 5.3,则应检查 DateTime。它允许您创建特定时间的对象,并且它有一个 sub() 方法,您可以使用该方法来减少时间。

If you are using PHP 5.3 you should check DateTime. It allows you to create an object for a specific time, and it has a sub() method that you can use to decrement the time.

孤单情人 2024-12-02 22:21:00
$now = time();
foreach ($items AS $item)
    {
        echo "\t\t<item>\n";
        echo "\t\t\t<pubDate>" . date("Y-m-d H:i:s", $now) . "</pubDate>\n" ;
        echo "\t\t</item>\n";
        $now -= (60*5);
    }
$now = time();
foreach ($items AS $item)
    {
        echo "\t\t<item>\n";
        echo "\t\t\t<pubDate>" . date("Y-m-d H:i:s", $now) . "</pubDate>\n" ;
        echo "\t\t</item>\n";
        $now -= (60*5);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文