[英]How to get values greater than or equal to in Mysql database sql when its a value like 1:00 PM?
I'm trying make an sql query that uses the users current time as a variable formatted like "11:30 AM". 我正在尝试创建一个SQL查询,它使用用户当前时间作为格式为“11:30 AM”的变量。 Then, finds records greater than equal to that in the mysql db. 然后,查找大于等于mysql db中的记录。
if(isset($_GET['curday'])){
$curday = $_GET['curday']; // users current day value 5 for Friday
}
if(isset($_GET['time'])){
$time = $_GET['time']; // users current time value 11:30AM
$time = preg_replace('/[^0-9]/', '', $time); // replacing the : and AM,PM
$query = "SELECT id, Name, Address, Meeting_type, Latitude, Longitude, Thetime, Gender, Dayofweek, 3956 * 2 *
ASIN(SQRT( POWER(SIN(($origLat - Latitude)*pi()/180/2),2)
+COS($origLat*pi()/180 )*COS(Latitude*pi()/180)
*POWER(SIN(($origLon-Longitude)*pi()/180/2),2)))
as Distance FROM $tableName WHERE
Longitude between ($origLon-$dist/cos(radians($origLat))*69)
and ($origLon+$dist/cos(radians($origLat))*69)
and Latitude between ($origLat-($dist/69))
and ($origLat+($dist/69))
having Distance < $dist AND Meeting_type = $id AND Dayofweek = $curday AND Thetime >= $time ORDER BY Thetime limit 100";
}
I know the AM and PM effect finding a >= value and possibly the : in the time value so I removed all but the numbers to try to help using $time = preg_replace('/[^0-9]/', '', $time); 我知道上午和下午效应找到一个> =值,可能还有:在时间值中,所以除了数字之外的所有数据都试图帮助使用$ time = preg_replace('/ [^ 0-9] /','' ,$ time); but still can't retrieve values properly. 但仍无法正确检索值。 I can't use timestamp in this existing database so would prefer a solution without. 我不能在这个现有的数据库中使用时间戳,所以更喜欢没有的解决方案。 It's local for now and I will put sql injection security in after I get this working. 它现在是本地的,我将把sql注入安全性放在我工作之后。
Thanks for any input! 感谢您的任何意见!
You can use DATE_FORMAT
mysql function to format your VAR_CHAR
column to date and apply your WHERE
clause to it. 您可以使用DATE_FORMAT
mysql函数将VAR_CHAR
列格式化为日期并将WHERE
子句应用于它。
In your case you will have a SELECT like this: 在你的情况下,你将有这样的SELECT:
SELECT * FROM your_table WHERE DATE_FORMAT(TheTime, "%H:%i %p") > your_value_formatted ORDER BY TheTime
Not the best way, but given what you have you could do: 不是最好的方式,但鉴于你有你可以做的:
REGEXP_REPLACE(Thetime, '[^0-9]', '') >= $time
But keep in mind that 12:00 PM
or 1200
in this case will be greater than 1:00 PM
or 100
, which is not correct. 但请记住,在这种情况下, 12:00 PM
或1200
点将大于1:00 PM
或100
,这是不正确的。 So you might want to convert to 24 hour time: 所以你可能想转换成24小时的时间:
$time = date('Hi', strtotime($time));
Then: 然后:
DATE_FORMAT(TheTime, '%H%i') >= $time
So from the previous example of 12:00 PM
and 1:00 PM
, you will have 1200
and 1300
. 因此,从前一个12:00 PM
和1:00 PM
示例,您将有1200
和1300
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.