php for loop for date increment 每次
php for loop for date increment each time
我想循环一个日期,以便每个时间日期都按前一个日期递增。我的代码在这里。请回复任何人,提前致谢
$today = date('Y-m-d');
for($i=1; $i<=4; $i++){
$repeat = strtotime("+2 day",strtotime($today));
echo $rdate = date('Y-m-d',$repeat);
}
我想要结果好像今天是 2016-04-04 比 2016-04-06, 2016-04-08, 2016-04-10, 2016-04-12.
实际上我想在用户输入提醒的地方做一个提醒日期。让用户今天想要添加提醒,并希望在 2 天、3 天或他想要的任何时间后,在下一天重复 5 次。比我如何用 for 循环重复日期。
你可以试试这个:
$today = date('Y-m-d');
for($i=1; $i<=8; $i++){
if($i%2 == 0){
$repeat = strtotime("+$i day",strtotime($today));
echo $rdate = date('Y-m-d',$repeat);
}
}
结果:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
在此示例中,您可以将 $i%2 == 0
与 limit <= 8
一起使用
试试这个:
<?php
$today = date('Y-m-d');
for($i=1; $i<=4; $i++)
{
$repeat = strtotime("+2 day",strtotime($today));
$today = date('Y-m-d',$repeat);
echo $today;
}
输出:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
使用基数为 2 的 for 循环,然后直接输出您的日期:
for( $i=2; $i<9; $i=$i+2 )
{
echo date('Y-m-d', strtotime( "+ $i days" )) . PHP_EOL;
}
结果:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
actually i want to make a reminder date where user enter reminder.
lets a user want to add reminder today and want repeat it 5 time after
2days, 3days or what ever he wants, in next comming day. than how i
repeat date with for loop.
我会帮忙解决以上问题。首先,我只想说我对 DateTime
对象有很大的个人偏好,而不是简单地使用 date
它更灵活,在我看来更具可读性,所以在处理日期时我总是建议在 date()
上使用它
所以这是一些代码:
$date = new DateTime(); // Pretend this is what the User entered. We got it via $_POST or something.
$interval = 2; // Repeat x times at y day intervals. (Not including the initial)
$repeatAmount = 2; // Repeat the reminder x times
for ($i = 0; $i <= $repeatAmount; ++$i) {
echo $date->format('d/m/Y');
$date->modify('+'. $interval .' day');
}
$date = new DateTime()
假设这是用户输入的日期,这是我们的起点,我们的第一个提醒会在这个时候。
$interval
和 $repeatAmount
是以天为单位的间隔,即在我们的示例 2 中,我希望每 2 天重复一次,以及您希望它重复的次数。
for ($i = 0; $i <= $repeatAmount; ++$i) {
我们想要循环多少次,用户说他们想重复多少次。小注 ++$i
在某些情况下往往比 $i++
的性能提升非常小,因此通常最好默认为它,除非您特别需要使用 $i++
echo $date->format('d/m/Y');
打印日期就行,提醒逻辑交给你。
$date->modify('+' . $interval . ' day');
按用户要求的时间间隔增加 dateTime
对象,在我们的例子中增加 2 天。
有任何问题请告诉我。
最简单的方法是什么答案
aslawin
下面的例子是通过日期
$begin = new DateTime($check_in);
$end = new DateTime($check_out);
$step = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $step, $end);
foreach ($period as $dt)
{
<sample code here>
}
我想循环一个日期,以便每个时间日期都按前一个日期递增。我的代码在这里。请回复任何人,提前致谢
$today = date('Y-m-d');
for($i=1; $i<=4; $i++){
$repeat = strtotime("+2 day",strtotime($today));
echo $rdate = date('Y-m-d',$repeat);
}
我想要结果好像今天是 2016-04-04 比 2016-04-06, 2016-04-08, 2016-04-10, 2016-04-12.
实际上我想在用户输入提醒的地方做一个提醒日期。让用户今天想要添加提醒,并希望在 2 天、3 天或他想要的任何时间后,在下一天重复 5 次。比我如何用 for 循环重复日期。
你可以试试这个:
$today = date('Y-m-d');
for($i=1; $i<=8; $i++){
if($i%2 == 0){
$repeat = strtotime("+$i day",strtotime($today));
echo $rdate = date('Y-m-d',$repeat);
}
}
结果:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
在此示例中,您可以将 $i%2 == 0
与 limit <= 8
试试这个:
<?php
$today = date('Y-m-d');
for($i=1; $i<=4; $i++)
{
$repeat = strtotime("+2 day",strtotime($today));
$today = date('Y-m-d',$repeat);
echo $today;
}
输出:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
使用基数为 2 的 for 循环,然后直接输出您的日期:
for( $i=2; $i<9; $i=$i+2 )
{
echo date('Y-m-d', strtotime( "+ $i days" )) . PHP_EOL;
}
结果:
2016-04-06
2016-04-08
2016-04-10
2016-04-12
actually i want to make a reminder date where user enter reminder. lets a user want to add reminder today and want repeat it 5 time after 2days, 3days or what ever he wants, in next comming day. than how i repeat date with for loop.
我会帮忙解决以上问题。首先,我只想说我对 DateTime
对象有很大的个人偏好,而不是简单地使用 date
它更灵活,在我看来更具可读性,所以在处理日期时我总是建议在 date()
所以这是一些代码:
$date = new DateTime(); // Pretend this is what the User entered. We got it via $_POST or something.
$interval = 2; // Repeat x times at y day intervals. (Not including the initial)
$repeatAmount = 2; // Repeat the reminder x times
for ($i = 0; $i <= $repeatAmount; ++$i) {
echo $date->format('d/m/Y');
$date->modify('+'. $interval .' day');
}
$date = new DateTime()
假设这是用户输入的日期,这是我们的起点,我们的第一个提醒会在这个时候。
$interval
和 $repeatAmount
是以天为单位的间隔,即在我们的示例 2 中,我希望每 2 天重复一次,以及您希望它重复的次数。
for ($i = 0; $i <= $repeatAmount; ++$i) {
我们想要循环多少次,用户说他们想重复多少次。小注 ++$i
在某些情况下往往比 $i++
的性能提升非常小,因此通常最好默认为它,除非您特别需要使用 $i++
echo $date->format('d/m/Y');
打印日期就行,提醒逻辑交给你。
$date->modify('+' . $interval . ' day');
按用户要求的时间间隔增加 dateTime
对象,在我们的例子中增加 2 天。
有任何问题请告诉我。
最简单的方法是什么答案
aslawin
下面的例子是通过日期
$begin = new DateTime($check_in);
$end = new DateTime($check_out);
$step = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $step, $end);
foreach ($period as $dt)
{
<sample code here>
}