douying9296 2015-01-21 09:04
浏览 81
已采纳

php将字符串转换为日期时间“2012年12月21日 - 03:45 PM”

i am using bootstrap date time picker to pick date and time . its giving me output "21 December 2012 - 03:45 PM" now i want to store it on MySQL database my problem is how can i store this string to MySQL data column (data type = date time).

here i using this code but its not working

$s = '21 December 2012 - 03:45 PM';

$date = strtotime($s);
echo date('d/M/Y:H:i:s', $date);

i want to store this string to mysql datbase in date time format. extract same string on sql query run

thank you

  • 写回答

5条回答 默认 最新

  • duanchou6534 2015-01-21 09:17
    关注

    If your input string format from that datepicker has like that:

    21 December 2012 - 03:45 PM
    

    You need to convert the format first that is applicable for your DATETIME field which is:

    YYYY-MM-DD HH:MM:SS
    

    The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format.

    Ref: http://dev.mysql.com/doc/refman/5.1/en/datetime.html

    From PHP, you could use the DateTime class. Provide the format that you have in your datepicker:

    $s = '21 December 2012 - 03:45 PM'; // input from date picker
    $date = DateTime::createFromFormat('d F Y - G:i a', $s);
    $insert_date = $date->format('Y-m-d H:i:s');
    echo $insert_date;
    // rest of your mysql insertion codes
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line