duanfei1987 2010-02-22 10:41
浏览 454
已采纳

如何将mySQL DATETIME类型转换为字符串以在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.

  • 写回答

2条回答 默认 最新

  • dongxu4580 2010-02-22 10:46
    关注

    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:

    1. Turn the date into a timestamp: $mytimestamp = strtotime($mydatetime); Strtotime can read a great number of time formats, DATETIME is among them. Manual on strtotime here

    2. Output the timestamp in any format you like: echo date("Y-m-d", $mytimestamp); Manual on date formatting options here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺