473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Query by Date Range

Hello theScripts, i've been using a lot of tutorials and forum reads here, so i figured it was time i joined. I've found everything here nothing but helpful.

I was wondering if you all could give me a hand on this. (if this is suppose to be in MySQL forum i apologize in advance).


I'm trying to make a "School Closing" alert for a school website, like when it snows. Pretty much, I made a simple form for a secretary to input into with a title and content. Her info goes into a MySQL table (named 'alert') with the fields title, content, datetime (which is set to timestamp with auto_update), and id (auto_increment).

Pretty much, i want the query to look at the table, if something was put in today, make a div on the site with the conent. If nothing was put in, it wont make a div. Simple.

Somehow, i need the query to check if the post was put in today between midnight and 11:59pm today. So once midnight hits tomorrow, it won't find anything.

I had this concept

[PHP]

<?php
$today = date("j, n, Y");
$query = mysql_query("SELECT * FROM `alert` WHERE datetime = $today ORDER BY `id` DESC LIMIT 0 , 1");
while($row = mysql_fetch_array($query)) {
echo '<div id="alert" style="background:red;border:2px solid #000;"><h1>'.$row['Title'].' - '.$row['datetime'].'</h1><p>'.$row['Alert'].'</p></div>';
}
?>[/PHP]


Now in thereory this should work, needless to say...it isn't though. I have the limit set to the first id because if she wanted to make a change to the current alert, all she has to do is retype and it will pick the top one.


Let me know what you all this, and thanks in advance.
Sep 13 '07 #1
10 2641
azang
3
whats the data type of 'datetime'?
If the type is datetime, I think you need to do this:

[PHP] $today = date("Y-m-d H:i:s");[/PHP]

[link removed]
Sep 13 '07 #2
the data type is timestamp.
Sep 13 '07 #3
If its a unix timestamp use the php time() function or the mysql function of UNIX_TIMESTAMP()
Sep 13 '07 #4
thanks, but what am i actually doing to set a range, from midnight to 11.59pm?
Sep 13 '07 #5
use something like (UNIX_TIMESTAMP()+(60*60*24))
Sep 13 '07 #6
pbmods
5,821 Expert 4TB
Heya, Record LL.

On the PHP side, you can build your two datetimes like this:
Expand|Select|Wrap|Line Numbers
  1. $thisMorning = date('Y-m-d 00:00:00');
  2. $thisEvening = date('Y-m-d 11:59:59');
  3.  
  4. // Or, for maximum efficiency...
  5. $today = date('Y-m-d');
  6. $thisMorning = $today . ' 00:00:00';
  7. $thisEvening = $today . ' 11:59:59';
  8.  
Then, using the MySQL BETWEEN keyword, it becomes reasonably easy to find the events of the day:
Expand|Select|Wrap|Line Numbers
  1. $_sql = "
  2. SELECT
  3.         *
  4.     FROM
  5.         `alert`
  6.     WHERE
  7.     (
  8.         `datetime`
  9.             BETWEEN
  10.                     '{$thisMorning}'
  11.                 AND
  12.                     '{$thisEvening}'
  13.     )
  14.     ORDER BY
  15.         `id` DESC
  16.     LIMIT 1";
  17.  
Sep 14 '07 #7
thanks alot, im still new to the whole "date formatting thing". im gonna try this out as soon as i can. thanks for the help all.
Sep 14 '07 #8
just an update, that worked. thank you
Sep 16 '07 #9
pbmods
5,821 Expert 4TB
Heya, LL.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Sep 16 '07 #10
ak1dnar
1,584 Expert 1GB
[Query by Date Range - First post here.]
Removed some unnecessary words from the thread title.
Sep 17 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Got2Go | last post by:
Hello Group, I have a table that has 3 columns: ID (int), datetime, Value(varchar) ID = ID for the SNMP device datetime = time record was added value = value added for that device. This...
1
by: Mike Cooper | last post by:
Hi everyone, This is a tough one. I have a database full of solicitations, identifying a customer and recording initial call, first followup, second followup, etc. My boss want to be able to...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
2
by: sixdeuce62 | last post by:
Hello, I am trying to create a query that will prompt me to enter the parameter value if beginning date and ending date. I have created everything I need in the query, but I have to manually go...
5
by: jennwilson | last post by:
Using Access 2000 - I have a query that is suppose to return the records from table within specified time range and find matching data from another table . Table houses Clinician name, location...
9
by: QCLee | last post by:
Sir can you help me to transfer my Access Query to MS excel? i have a command button on the form to export the parameter query named "HVACWindwardQuery" to excel spreadsheet and i got the codes...
4
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
2
by: lindabaldwin | last post by:
Hello everyone, I am fairly new to VBA. I have a worksheet in Excel, named "Data Sheet" from which I am trying to query data. This worksheet contains the following data: unit (column A), date...
11
by: lenygold via DBMonster.com | last post by:
Hi everybody! This query is supposed to count consecutive years from the current year without OLAP. Input Table: ID DateCol 1 02/01/2006 1 01/01/2006 1 01/01/2005
19
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.