doubeng9567
2015-11-04 15:40
浏览 138
已采纳

使用php获取下一个第15天和/或第30天的日期?

I require to get the dates of the upcoming 15th and 30ths of the next months as for the current date. (If February is within range it must be 28/29th of course).

Can I do this using mktime/strtotime or maybe using another method?

I got this but of course this is for only get the last day of this and next month. I need the upcoming 15ths and 30ths instead.

$cuota1 = date('t-m-Y', strtotime('+15 days'));
    return $cuota1;

$cuota2 = date('t-m-Y', strtotime('+30 days'));
    return $cuota2;

$cuota3 = date('t-m-Y', strtotime('+45 days'));
    return $cuota3;

Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我需要获取即将到来的 15th 30ths的日期。 (如果二月在范围内,当然必须是28/29)。

我可以使用mktime / strtotime或者使用其他方法吗? < 我得到了这个,但当然这只是为了这个和下个月的最后一天。 我需要即将到来的15日和30日。

  $ cuota1 = date('tm-Y',strtotime('+ 15 days')); 
返回$ cuota1;  
 
 $ cuota2 =日期('tm-Y',strtotime('+ 30天')); 
返回$ cuota2; 
 
 $ cuota3 =日期('tm-Y',strtotime('+  45天')); 
返回$ cuota3; 
   
 
 

提前致谢。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dpwo36915 2015-11-04 16:07
    已采纳

    Hope I've got your idea right, and may be this solution is quite long, but looks like it's bullet-proof:

    $numOfDays = date('t', $todayStamp);
    $base = strtotime('+'.$numOfDays.' days', strtotime(date('Y-m-01', $todayStamp)));
    $day15 = date('Y-m-15', $base);
    $day30 = date('Y-m-'.min(date('t', $base), 30), $base);
    

    where $todayStamp is generally the value of time(), but for debug purposes it can be strtotime() of an arbitrary date. For example, let's take "difficult" case of the next leap year:

    $today = '2016-01-30';
    $todayStamp = strtotime($today);
    $numOfDays = date('t', $todayStamp);
    $base = strtotime('+'.$numOfDays.' days', strtotime(date('Y-m-01', $todayStamp)));
    $day15 = date('Y-m-15', $base);
    $day30 = date('Y-m-'.min(date('t', $base), 30), $base);
    var_dump($day15);
    var_dump($day30);
    

    The output is

    string(10) "2016-02-15"
    string(10) "2016-02-29"
    

    and for 2016-02-29 the output will be

    string(10) "2016-03-15"
    string(10) "2016-03-30"
    
    点赞 评论
  • douzhuang2016 2015-11-04 15:53

    Perhaps a little crude but you could try something like this, though of course this doesn't validate the date at all:-

    $m=date('m')+1;
    $y=date('Y');
    
    $d=15;
    echo date( 't-m-Y', strtotime( "{$d}.{$m}.{$y}" ) );
    # > outputs: 31-12-2015
    
    $d=30;
    echo date( 't-m-Y', strtotime( "{$d}.{$m}.{$y}" ) );
    # > outputs: 31-12-2015
    
    点赞 评论
  • duanliusong6395 2015-11-04 17:14
    $tstamp =  strtotime('+1 month');  //add a month
    $m=date('m',$tstamp);   //month value
    $y=date('Y');  //this year
    $date15 = date("Y-m-d", strtotime("$y-$m-"."15"));  //15 th of next month
    $date31 = date("Y-m-t", strtotime("$y-$m-"."15")); //last date of next month
    

    This will give you last date of next month.

    点赞 评论

相关推荐 更多相似问题