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

使用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.

  • 写回答

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"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架