Php 查找日期的下一个匹配项

Php 查找日期的下一个匹配项,php,Php,我想从当前日期中查找下一个出现的日期 例如,假设我想从当前日期开始查找本月20日 如果当前日期为10月10日,则返回结果2014-10-20(Y-m-d) 如果当前日期是10月22日,则返回结果2014-11-20(Y-m-d)我刚刚使用while循环创建了一个解决方案 $oldd= "2014-06-20"; $newdate = date("Y-m-d",strtotime($oldd."+1months")); while(strtotime($newdate) <= strto

我想从当前日期中查找下一个出现的日期

例如,假设我想从当前日期开始查找本月20日

如果当前日期为10月10日,则返回结果2014-10-20(Y-m-d)
如果当前日期是10月22日,则返回结果2014-11-20(Y-m-d)

我刚刚使用while循环创建了一个解决方案

$oldd= "2014-06-20";

$newdate = date("Y-m-d",strtotime($oldd."+1months"));

while(strtotime($newdate) <= strtotime(date("Y-m-d")))
{
    $newdate = date("Y-m-d",strtotime($newdate."+1months"));
}
echo $newdate;
$oldd=“2014-06-20”;
$newdate=日期(“Y-m-d”,标准时间($oldd.+1个月”);

while(strotime($newdate),并将其传递给strotime()。需要从参考时间字符串中提取的时间信息。如下所示:

$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);

echo date('Y-m-d H:i:s',
    strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
使用字符串连接可以获得相同的结果:

echo date('Y-m-d', strtotime("next Thursday", $timestamp)
    . ' ' . date('H:i:s', $timestamp);

另一种方法是,您可以使用
DateTime
object的方法,PHP在处理日期时间方面有非常丰富的API

$current_date = new DateTime('2014-06-20');

if ($current_date->format('d') >= 20) {
    // $current_date->modify('last day of this month')->modify("+20 days");
    $current_date->modify('first day of next month')->modify("+19 days");
}else{
    $current_date->modify('first day of this month')->modify("+19 days");
}
echo $current_date->format("Y-m-d");

如果当前日期>请求的日期,则增加1…2014-11-20的月份不是20october@deceze假设我有一个日期2014年7月20日,当前日期是2014年10月10日,现在结果应该是2014年10月20日。我正在制定一个租金系统,我想将从上次收到租金之日起的所有租金更新到当前mo的下一个日期n.这仍然很简单…你只需要一些比较运算和一点数学知识。请你自己试着解决它。@deceze我刚才确实提出了一个解决方案。那么还有其他好方法吗?$oldd=date(“Y-m-d”,strotime(“2014-06-20”);$newdate=date(“Y-m-d”,strotime($oldd.+1个月”);而(标准时间($newdate)