douxian7808 2018-06-28 09:19
浏览 705
已采纳

strtotime(+1月)和12月/明年的行为

I am working with php and have to compare some data created between the first of the current month and the first of the next month.

I use date('Y-m-d', strtotime('+1 month')); to get the first of the next month. Does this mean that I get the January 1st 2019 when the current date is e.g. 2018-12-16? Or does strtotime('+1 month') just adds the next month and ignores the year, so the result would be 2018-01-16...?

I am not sure, if this is the way strtotime('+1 month') works and I can't test it with a hardcoded month; date('Y-12-d', strtotime('+1 month')); returns 2018-12-28... Did I even test it in the correct way?

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • dsozqcx9668 2018-06-28 09:25
    关注

    strtotime('+1 month') add the 1 month in current month, for example

    if current date is 2018-06-28 then

    echo date('Y-m-d', strtotime('+1 month'));
    

    Output is :

    2018-07-28
    

    If you want first date of month then use :

    echo date('Y-m-01', strtotime('+1 month')); // +1 month (Jun to July)
    

    or

    echo date('Y-m-01'); // Return Current Month
    

    If current month is December and you add 1 month with strtotime('+1 month') then your date will be in next year

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了