dou47278 2018-01-19 01:57
浏览 31
已采纳

在php的倒数第二个月

So far with

strtotime('last day of this month');

gives me the last day (31), but I want the penultimate (30), and I've tried with:

strtotime('-1 day','last day of this month');

Also with strtotime('last day of this month') - strtotime('1 day'); and other things. Always results in (formatted with date()) 31-dec-1969.

How can I achieve this?

  • 写回答

1条回答 默认 最新

  • duanquanzhi5560 2018-01-19 02:04
    关注

    You're almost there. strtotime() expects parameter 2 to be an integer in case of subtracting a time span from a date. So you need to

    1. convert the last day of the month to an integer timestamp value using strtotime('last day of this month')
    2. Then subtract a day from it by doing strtotime('-1 day', $valueFromStep1)

    Hence doing

    strtotime('-1 day',strtotime('last day of this month'))

    Gives the expected answer

    Here is a similar question to the one you have asked: Subtract 1 day with PHP

    strtotime reference: http://php.net/manual/en/function.strtotime.php

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?