根据2个数字之间的差异多次插入图像。 PHP [英] Inserting an image multiple times based on difference between 2 numbers. PHP
本文介绍了根据2个数字之间的差异多次插入图像。 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
处理脚本,计算从API检索的日期之间的差异。我想让它然后放入图像的次数差异。
working on a script, that calculates the difference between dates retrieved from an API. I want it to then drop in an image as many times as the difference.
$d1 = new DateTime('2012-04-04');
$d2 = new DateTime('2012-03-31');
$interval = $d1->diff($d2);
我曾尝试使用for循环,但没有成功
Ive tried to have a go with a for loop but with no success
for ($i = 0 $i <= $interval; $i++)
{
echo = "<img src=\"test.jpg"/";
}
你们有没有看到任何问题?
can you guys see any problems?
推荐答案
我发现PHP的DateTime在使用modify()方法而不是普通算术时效果更好。
假设你每天想要一个图像:
I find PHP's DateTime works better when you use the modify() method instead of normal arithmetic.
Assuming you want one image per day:
$d1 = new DateTime('2012-04-04');
$d2 = new DateTime('2012-03-31');
while($d2 <= $d1)
{
echo "<img src=\"test.jpg\">";
$d2->modify("+1 day");
}
这篇关于根据2个数字之间的差异多次插入图像。 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文