How do I convert a mySQL DATETIME type to a string for use in Javascript?
source link: https://www.codesd.com/item/how-do-i-convert-a-mysql-datetime-type-to-a-string-for-use-in-javascript.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How do I convert a mySQL DATETIME type to a string for use in Javascript?
I want to display a DATETIME I get from a mySQL database as a String in Javascript.
I'm using PHP to put the DATETIME into a variable: $mydatetime.
It displays fine on the PHP page, but not in my javascript function.
<?php
echo $mydatetime --> 2010-04-19 13:00:00
echo "<script language=javascript>myfunction($mydatetime);</script>";
?>
Javascript
function myfunction(mydatetime) {
alert(mydatetime);
}
This produces an error in my console: Uncaught SyntaxError: Unexpected number
I've tried many things to try to convert mydatetime to a string, but nothing seems to be working.
Any help is appreciated, thanks.
I don't know what format you need for your function - you don't specify. Chances are you just need to enclose the value in quotes:
echo "<script language=javascript>myfunction('$mydatetime');</script>";
But the general way to convert dates from mySQL to something else in PHP is:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK