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.