我正在尝试使用用户当前时间作为格式为“11:30 AM”的变量进行 sql 查询。然后,查找大于等于 mysql 数据库中的记录。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";}我知道 AM 和 PM 效应会在时间值中找到 >= 值和可能的 : 所以我删除了所有但数字以尝试帮助使用 $time = preg_replace('/[^0-9]/', '' , $time); 但仍然无法正确检索值。我不能在这个现有的数据库中使用时间戳,所以更喜欢没有的解决方案。它现在是本地的,我会在完成这项工作后加入 sql 注入安全性。感谢您提供任何意见!
                    
                    
                2 回答
 
                    
                    
                            胡子哥哥
                            
                                
                            
                        
                        
                                                
                    TA贡献1825条经验 获得超6个赞
您可以使用DATE_FORMATmysql 函数将您的VAR_CHAR列格式化为日期并将您的WHERE子句应用于它。
在您的情况下,您将有一个这样的选择:
SELECT * FROM your_table WHERE DATE_FORMAT(TheTime, "%H:%i %p") > your_value_formatted ORDER BY TheTime
 
                    
                    
                            冉冉说
                            
                                
                            
                        
                        
                                                
                    TA贡献1877条经验 获得超1个赞
不是最好的方法,但考虑到你可以做的事情:
REGEXP_REPLACE(Thetime, '[^0-9]', '') >= $time
但请记住,12:00 PMor1200在这种情况下将大于1:00 PMor 100,这是不正确的。所以你可能想转换为 24 小时制:
$time = date('Hi', strtotime($time));然后:
DATE_FORMAT(TheTime, '%H%i') >= $time
因此,从前面的12:00 PMand示例中1:00 PM,您将拥有1200and 1300。
- 2 回答
- 0 关注
- 237 浏览
添加回答
举报
0/150
	提交
		取消
	