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 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面