我是php的新编码,我有一个关于在循环中增加日期的查询,这是通过调用weather API(http://openweathermap.org)生成的,我使用PHP curl返回XML然后我循环浏览3组数据3天,第一天是当天,然后接下来的两个是接下来的两个连续日期。
我认为最简单的方法是保存当前日期,然后每次代码循环时递增此日期,如下所示:
if($numOfItems>0){ // yes, some items were retrieved
foreach($items as $current){ //open loop
?>
<div style="width:33%;float:left;position:relative;padding:2%" class="WeatherSection">
<?php
$myDate = date('d/m/Y');
echo $myDate;
var $day = date('d++');
$day = 'd';
$summary = $current->symbol['name'];
echo "<p>$summary</p>";
?>
</div>
<?php
} //close loop
} //close code
&GT;
可悲的是,这似乎不起作用,我确信对于有更多PHP经验的人来说这是一个简单的修复,但我无法弄清楚为什么这不起作用...非常感谢任何指导! / p>
已编辑:感谢您的帮助,我尝试添加此代码:
$time = time();
echo date( 'd/m/Y', $time);
$time = strtotime('+1 day', $time);
echo date( 'd/m/Y', $time);
$time = strtotime('+1 day', $time);
echo date( 'd/m/Y', $time);
但它循环显示所有这些日期3次,请参见截图... 我只希望日期显示一次,递增下一个循环并再次显示新日期然后再次显示。有一种简单的方法可以做到这一点吗?
编辑:在查看选项之后,似乎尝试从XML收集日期可能是最好的选择,虽然PHP curl收集数据的方式,但似乎我无法在时间内获取数据”。这是我的新代码:
<?php
$ request = //这是添加我的API密钥的地方,已删除以放入StackOverflow
$crl = curl_init(); // creating a curl object
$timeout = 10;
curl_setopt ($crl, CURLOPT_URL,$request);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$xml_to_parse = curl_exec($crl);
curl_close($crl); // closing the curl object
$parsed_xml = simplexml_load_string($xml_to_parse);
// traversing the xml nodes to count how many days were retrieved
// ignore the first one (weatherdata)
$items = $parsed_xml->forecast->time;
$numOfItems = count($items);
if($numOfItems>0){ // yes, some items were retrieved
foreach($items as $current){ //open loop
?>
<div style="width:30%;float:left;position:relative;padding:1.5%;margin-top:10px;text-align:center;" class="WeatherSection">
<?php
$dateofweather = strval($current->time['day']); //my attempt to retrieve data from the XML
echo "<p>$dateofweather</p>";
$summary = $current->symbol['name'];
echo "<p>$summary</p>";
$icon = $current->symbol['var'];
echo "<p><img src='http://localhost:8888/images/weathericons/$icon.png'/></p>";
$rain = intval($current->precipitation['value']);
if($rain > 5) { echo "<div class='orange_background'><h5 style='padding:10px'>Very Rainy! Remember to regularly get your roof checked!</h5></div>"; }
?>
</div>
<div style="width:1px;height:300px;float:left;position:relative;padding:0;margin:5px;margin-top:10px;background:#fff;"></div>
<?php
} //close loop
} //close code
&GT;
答案 0 :(得分:0)
这样的事情怎么样?
$my_date = time();
echo date( 'd/m/Y', $my_date);
$my_date += (24 * 3600); // 24 hours of 3600 seconds
$day = date( 'd', $my_date );
答案 1 :(得分:0)
您可以使用mktime功能:
$today = mktime(0, 0, 1, date('m'), date('d'), date('Y'));
$today_date = date('d/m/Y', $today);
$tomorrow = mktime(0, 0, 1, date('m'), date('d') + 1, date('Y'));
$tomorrow_date = date('d/m/Y', $tomorrow);
答案 2 :(得分:0)
在函数date('d') + 1
中使用mktime
是安全的,但最好使用strtotime
代替..
$time = time();
echo date( 'd/m/Y', $time);
$time = strtotime('+1 day', $time);
echo date( 'd/m/Y', $time);
<强>更新强>
$time = time();
for ($i = 0; $i < 3; $i++) {
echo date( 'd/m/Y', $time);
$time = strtotime('+1 day', $time);
}
答案 3 :(得分:0)
您可以使用php对象add function
$date = new DateTime('2016-01-01');
$date->add(new DateInterval('P1D'));
echo $date->format('Y-m-d') . "\n";
或程序
$date = date_create('2016-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');