donglang8008 2016-05-23 19:46
浏览 79

String hh:mm to date.sql hh:mm(Android,Java)或String to Time(PHP)

In my getRequest i'm passing two Strings (minTime and maxTime), in MySQL I have two columns TIME (minTime and maxTime) I need all the registers between these two values.

For that reason I need to convert minTime and maxTime to sql.date and retrieve the proper registers (with strings is not working properly). I'm trying many things without success, for example:

String minTime_user = "10:45"

java.util.Date utilDate = new SimpleDateFormat("hh:mm").parse(minTime_user);

//output: Thu Jan 01 10:28:00 GMT-05:00 1970

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

//output: 1970-01-01

If not possible, how can I convert String to Time in PHP? I've seen the function strtotime but it returns the number of seconds since 1970, ideally I prefer the format "hh:mm" to be able to compare without having to convert the values of my table to Unix.

  • 写回答

1条回答 默认 最新

  • doulan3436 2016-05-24 00:06
    关注

    If, as you say, your time is stored in a TIME column, you can compare times directly in a MySQL query:

    SELECT * FROM table WHERE column BETWEEN "10:45" AND "15:44";
    

    See here for a demo.

    PHP is a loosely typed language, so you have no need to worry about converting it to anything; it's sent to MySQL as a string.

    Edit: the documentation recommends explicitly casting the values to TIME within the query, so you'd end up with something like this:

    SELECT * FROM table WHERE column BETWEEN CAST("10:45" AS TIME) AND CAST("15:44" AS TIME);
    

    Though I've never had any problems using this function without the casting.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端