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
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.

Link to comment
Share on other sites

$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.


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.