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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料