-1

I am inserting

$t= "<script>
document.write(new Date().toLocaleTimeString()+' '+new Date().toLocaleDateString())
</script>";

into the database . echo $t gives the correct data and time but while inserting into the database , it is inserted as

"<script>
document.write(new Date().toLocaleTimeString()+' '+new Date().toLocaleDateString())
</script>";

?

why does it so ? Also, The column where, I am inserting this $t is of varchar type.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
shub-naik
  • 21
  • 1
  • 2
  • 1
    Can you show your insert code? – Dilip Hirapara Sep 22 '19 at 09:46
  • 1
    This is JavaScript. Your question is very unclear. Can you show us your code? – Dharman Sep 22 '19 at 11:28
  • 1
    Echoing it gives the correct result because the echo command outputs the JavaScript code to your browser. The browser then executes the JavaScript, because browsers have the ability to do that. When you insert it into the database there is no browser to execute the script first... instead it's just seen as some meaningless text. What do you actually want to do? If you just want to output today's date, both PHP and SQL can do that task, no need for JavaScript. Please clarify your intention – ADyson Sep 22 '19 at 20:28
  • 1
    When you have this value saved in the variable `$t` and insert it into the database, then this value is stored in the database. Why do you expect something different than the value you want to store being stored as exactly that? – Progman Sep 22 '19 at 20:29

1 Answers1

2

You're explicitly requesting that string to be stored in the DB, not the result. Try $t=date('Y-m-d');

Modify the date format to match your regional requirement

Shane Weeks
  • 77
  • 1
  • 4