dongzhou4727 2019-07-26 01:29
浏览 59
已采纳

为什么在运行日期操作时不能用日期变量替换日期变量?

In php command to calculate date operation.

php > echo $d = date('Y-m-d');
2019-07-26
php > echo date("Y-m-d",strtotime("$d -1 day"));
2019-07-25

With above php code,Date 2019-07-26 minus 1 day = 2019-07-25.
Why can't combine the two command as one such as following php code?

php > echo date("Y-m-d",strtotime("date('Y-m-d')  -1 day"));
1970-01-01

Why can't replace date variable $d with it's expression date('Y-m-d')?

  • 写回答

1条回答 默认 最新

  • duankuangxie9070 2019-07-26 02:03
    关注

    This is a string "date('Y-m-d') -1 day" -- for obvious security reasons, functions contained within strings do not execute automatically ... (you can with eval but that's another topic for another day). Otherwise if they did you could hijack a server very easily, in fact many exploits use things like eval() or the e modifier for Regex ( which is now removed as of PHP7, for this very reason ) to execute arbitrary code contained within strings via PHP.

    So just ask yourself what is the time in seconds since 1970 to the literal string "date('Y-m-d') -1 day" if you answered I have no idea, well neither does PHP.

    Personally, I would ditch the procedural functions for this and do this:

    echo (new DateTime)->modify('-1 day')->format('Y-m-d'); 
    

    Output

    2019-07-24
    

    Sandbox

    If you really want to use the Date function and procedural style, then this will do the job

     echo date('Y-m-d', strtotime(date('Y-m-d').' -1 day'));
    

    Here I am concatenating (.) the output of the inner date function call with the string ' -1 day' which looks like this 2019-07-25 -1 day when it's all put together by PHP. This is something the date functions understand and will work.

    What probably got you (and one other thing to understand) is variable interpolation (variables within double quotes get replaced by PHP).

       date("Y-m-d",strtotime("$d -1 day"));  //$d is replaced by value
    

    I like to use the {} when doing this, it's a bit easier to read (basically it looks better in my IDE) and you can use it with things like properties etc. like this: echo "foo {$this->name}", which wont work without the brackets. Methods of objects can also work with the brackets such as $D=new DateTime; echo "today is {$D->format('Y-m-d')}".

    Now, unfortunately the date function is not a variable, so interpolation does not apply to it (for the reasons I mentioned above):

        "date('Y-m-d')  -1 day"
    

    So you see in the first example that you have with $d the end result of that is something like this 2019-07-25 -1 day - which is pretty much what I have above... Before (your second example) is what is in the code block above (literally).

    Cheers!

    PS. I like saying "interpolation" because it makes me sound smart... lol.

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)