简体   繁体   English

当它的值如下午1:00时,如何在Mysql数据库sql中获取大于或等于的值?

[英]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 PM1200点将大于1:00 PM100 ,这是不正确的。 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 PM1:00 PM示例,您将有12001300

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在下午5点获取PHP的当前时间? - How to get Current time in php like 5:00 pm? MySQL查询-获取“大于…”的列值 - MySQL query - get column values “greater than…” 仅当字段中的值等于或大于输入值时,才如何从MySQL表的值中减去用户输入值? - How can I subtract a user input value from a value in a MySQL table only if the value in the field is equal to or greater than the input value? 如何计算数组中的值并获取等于PHP值的值? - How to count the values in an array and get the value if its equal to a number PHP? PHP, SQL 检查值是否大于或小于数据库值 - PHP, SQL Check if the value is greater or less than the database value 如何在日期/时间mysql字段上执行循环检查以获取不等于“ 00:00”的时间值? - How to perform a looping check on date/time mysql field for time values NOT equal to “00:00”? 如果使用AJAX或XMLHttprequest值大于0,则将数据保存到MySQL数据库中 - Save data into MySQL database if value is greater than 0 using AJAX or XMLHttprequest 如何在mysql表中获得大于我的最大值的最大值 - How to get a max value greater than my max value in mysql table MySQL:选择值最接近但大于给定值的行 - MySQL: Select rows with values nearest but greater than a given value 获取的数据大于等于今天的日期 - get the data is greater than is equal todays date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM