按钮的Onclick在HTML文件中工作,但在PHP文件中的HTML中不起作用

I'm very new to PHP, JS, and HTML so I hope you don't mind if the question is very trivial. So I basically have the following code, the onlick would work in a separate HTML file but it will not work if I put this in an echo of a PHP file or put it in a separate html block inside a PHP file.

button.html (this works)

   <body>


        <p>Click the button to display the time.</p>

        <button onclick="getElementById('demo').innerHTML=Date()">What is the time?</button>

        <p id="demo"> </p>

    </body>

</html>

Here is the echo code:

button.php (doesn't work)

<?php
....
....
echo "

        <input type ='button' value = 'Date'  onclick = 'getElementById('demo')=Date();' /> 

        <p id='demo'> </p>

        "
?>

This is the HTML code inside PHP file that doesn't work.

button.php (doesn't work)

<?php>

?>

<html>
<body>

<p>Click the button to display the time.</p>

<button onclick="getElementById('demo').innerHTML=Date()">What is the time?</button>

<p id="demo"></p>

</body>
</html>

Any help would be appreciated!

This is what i have tried Hope this helps

<!DOCTYPE html>
<html>
<body>

<h1>My First JavaScript</h1>

<button type="button"
onclick="document.getElementById('date_time_button').innerHTML = Date()">
Click and see Date and Time.</button>

<p id="date_time_button"></p>

</body>
</html> 

</div>

Try this in a single echo

echo ' <input type ="button" value = "Date"  onclick = "getElementById('demo')=Date();" />';

The single and double apostrophe sometimes are causing the error and PHP cannot understand which is what

You cannot have two elements on an HTML page with same ID. Try code below.

<html>
  <body>
     <p>Click the button to display the time.</p>
     <button onclick="getElementById('demo').innerHTML=Date()">What is the time?</button>

     <p id="demo"> </p>
     <?php 
        echo ' <input type ="button" value = "Date"  onclick = getElementById("demo").innerHTML=Date(); />';  
     ?>
  </body>
</html>

Also as mentioned by @WTFZane that The single and double apostrophe sometimes are causing the error and PHP cannot understand which is what

You cannot write inside a string the same quote which it's embracing the string.

You need to escape that quote with the \ character like this code:

echo "

        <input type ='button' value = 'Date'  onclick = \"getElementById('demo')=Date();\" /> 

        <p id='demo'> </p>

        ";
?>

The echo line need a ; for ending it too ;-)

Php quote Problem face here. I think you can wrong here ('demo').

<p id="demo"> </p> 
<button onclick="getElementById('demo').innerHTML=Date()">What is the time?</button>

Replace With this code

<?php echo '<p id="demo"> </p><button onclick="getElementById(\'demo\').innerHTML=Date()">What is the time?</button>';?>