admin管理员组文章数量:1434949
i have a date entry in yyyy-mm-dd format in mysql database. Using php i could get the date value
$row = $myresult['date'];
now i wish to alert this value in javascript
so i tried
var temp_date = <?php echo($row)?>;
alert (temp_date);
but that didn't work for me. The problem i feel i am facing is to maintain the string format of the datestring, which seems to be getting lost when used in javascript. Any help?
i have a date entry in yyyy-mm-dd format in mysql database. Using php i could get the date value
$row = $myresult['date'];
now i wish to alert this value in javascript
so i tried
var temp_date = <?php echo($row)?>;
alert (temp_date);
but that didn't work for me. The problem i feel i am facing is to maintain the string format of the datestring, which seems to be getting lost when used in javascript. Any help?
Share Improve this question asked Aug 3, 2009 at 14:30 Anirudh GoelAnirudh Goel 4,73119 gold badges83 silver badges111 bronze badges 1- As the fifth in a row to provide the very same answer allow me to withdraw mine. – Boldewyn Commented Aug 3, 2009 at 14:36
6 Answers
Reset to default 4String literals are sorrounded by double or single quotes in JavaScript:
var temp_date = "<?php echo($row)?>";
alert (temp_date);
This answer originally was a quasi exact copy of the other four first.
If you need the date information as such, you could use Unix timestamps:
SELECT UNIX_TIMESTAMP(timefield) AS timestamp WHERE id = 1;
then just echo it in PHP into this JS snippet:
var mydate = new Date(<?php echo $row['timestamp']*1000; ?>);
You don't need quotes here, since you echo a number. This allows for, e.g.,
alert (mydate.getDate());
and the such.
Cheers,
Use json_encode(). This will turn PHP variables into valid javascript, so you don't have to worry about quoting/escaping etc.
var temp_date = <?php echo json_encode($row); ?>;
alert (temp_date);
You have to add quotes to your JavaScript variable assignation:
var temp_date = '<?php echo($row)?>';
alert (temp_date);
var temp_date = '<?php echo($row)?>';
alert (temp_date);
Add quotes. Otherwise it will error because strings without quotes are troublesome.
.php
$smarty->assign("profile_date",date("Y/m/d"));
.tpl
var NowDate=new Date("{/literal}{$profile_date}{literal}");
本文标签: use php date string in javascriptStack Overflow
版权声明:本文标题:use php date string in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745638484a2667707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论