Jump to content

MySQL query and Time


topflight

Recommended Posts

Hello I have a table called called flights where it has all my flights info. But in that table I have a column called time. I want to put pull a random 15 flights who time is 30mins from the current time. For instance if the current time is 4:30 I want to pull a random 15 rows who time is 35 mins from now(Which is 4:30) how can I do that.

 

I have reccevied help, and someone provided the following query

 

select * from yourtable where timecolumn between date_format(now(),'%H%i') and addtime(date_format(now(),'%H%i'),30) order by rand() limit 15

 

So then I edit the code to look like this

 

<?php	include'config.php';	$get = mysql_query("SELECT * from `dep` WHERE dtime between date_format(now(),'%H%i') and addtime(date_format(now(),'%H%i'),30) order by rand() limit 15");?><table width="100%"><?while($row = mysql_fetch_assoc($get)){?><td><? echo "$row[dtime]";?></td><?php}?></table>

 

 

In the database the time does not have a ":" between the hours and mins, inseated they look like this 1730 instead of 1730 and the time is in 24 hour. Please help.

 

Thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/191795-mysql-query-and-time/
Share on other sites

not with that query. it is assuming you are using a datetime or timestamp field.

 

try

SELECT * from `dep` WHERE dtime >= $time_minus_thirty AND dtime <= $time_plus_thirty order by rand() limit 15

 

you going to need to create time_minus_thirty and time_plus_thirty by yourself using php.

$hour = date('G');
$minute = date('i');

//add 30 to minutes. if its over 60 minus 60 off it and add one to hour. if hour is then over 23 set to 1. similar for minus 30 minutes.

//put your times together
$time_string = $hour . $minute;

//now use $time_string in your query.


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.