php:如何将每个项目的日期时间递减到 foreach 中...?
...而且不仅仅是一次...
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你最初的减去(60*5)的想法是完全正确的;您只需要不断从工作值中减去它,而不是按照代码重复从原始值中减去它:
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:
如果您使用 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 asub()
method that you can use to decrement the time.