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 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用