duandao1931 2016-08-09 14:28
浏览 48

只有PHP strtotime的未来日期()

PHP's strtotime() uses the current year by default. How to get only future dates?

echo date('l d. M Y', strtotime('first sunday of april')); // Sunday 03. Apr 2016

I can't manage to get the next first Sunday of April. The date must not be in the past and it must be always relative (no 2017 or 2018 hardcoded).

echo date('l d. M Y', strtotime('first sunday of next april')); // fails

echo date('l d. M Y', strtotime('first sunday of april next year')); // wrong from January until that Sunday in April

I think I could do it in multiple steps or create a function to check, if current time is before/after the first Sunday and insert a 'next year' at the end.

But I was wondering if there is a simple solution with strtotime()

  • 写回答

3条回答 默认 最新

  • dongtuo5611 2016-08-09 14:37
    关注

    I dont think this is particularly elegant, but it works, and I hope it is what you were looking for?

    echo date('l d. M Y', strtotime('first sunday of april', strtotime('first day of next year')));
    

    However this seems like a much better, maintainable, readable solution

    $d = new DateTime();
    $d->modify( 'first day of next year');
    echo $d->format('l d. M Y') . PHP_EOL;
    $d->modify( 'first sunday of april');
    echo $d->format('l d. M Y') . PHP_EOL;
    

    Which gives

    Tuesday 01. Aug 2017
    Sunday 02. Apr 2017
    

    The echo of the year change date, you would not need to do, its there just to prove that the year changed

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用