[英]Looping from a start date to an end date
I'd like to make a selection list of dates that a user could choose from. 我想列出用户可以选择的日期 。 The date should start by the Current date today and ends 5 days after .
日期应从今天的当前日期开始,到的5天后结束 。 I just don't know how to execute it inside a loop so I wouldn't change the date over and over again.
我只是不知道如何在循环内执行它,所以我不会一遍又一遍地更改日期。 Thanks.
谢谢。 Please help.
请帮忙。
<?php
$today = date('d/m/y');
for($i=0; $i<5; $i++) {
$nextDay = date('m/d/y', time() + (86400*$i));
//make something with your date
}
?>
Do you mean something like this? 你的意思是这样吗?
echo "<select name=\"date\">\n";
for($i=time();$i<time()+5*24*60*60;$i+=24*60*60) {
$d = date('Y-m-d', $i);
echo echo "<option value=\"$d\">$d</option>\n";
}
echo "</select>\n";
Quick and dirty way: 快速而肮脏的方式:
for ($i = 0; $i < 5; $i++){
print date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + $i, date('Y')));
print "\n";
}
You can use a UNIX timestamp to store the time and loop. 您可以使用UNIX时间戳来存储时间和循环。
Use date('Ym-d', $timestamp)
to generate a string of date. 使用
date('Ym-d', $timestamp)
生成日期字符串。
Use strtotime($string)
to convert a Ymd string to timestamp integer. 使用
strtotime($string)
将Ymd字符串转换为时间戳整数。
$now = time();
for($i = $now; $i < $now + 5*86400; $i=$i+86400)
{
$Ymd = date('Y-m-d', $i);
//do other things here.
}
You can use the php DateTime functions for this: 您可以为此使用php DateTime函数:
<?php
$date = new DateTime(); // Without argument: now
$interval = new Dateinterval("P1D"); // Period: 1 day
for ($i = 0; $i <= 5; $i++) {
echo $date->format("Y-m-d")."\n";
$date->add($interval);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.