Jump to content

anybody good with querying dates?


dadamssg87

Recommended Posts

I have events stored in my database with start(datetime) and end(datetime). I want to query a range of two dates and count how many events fall between the range.

 

I have no idea where to begin constructing this kind of query. Any help would be awesome.

 

Link to comment
Share on other sites

thanks for the quick response. i'm a little confused where i put the start and end date of the range i want to query.

 

I'm not sure what 'field' would be in "COUNT(field)", if i obviously want to count the entire row. Also 'datetime_field'?

 

For instance let's say i have the following

<?php
$input_start = "2011-05-02";
$input_end = "2011-05-07";
$table_name = "Bookings";

 

my table structure is as follows

start 	datetime
end datetime
id int(11)
name varchar(100)

 

 

 

SELECT COUNT(field) AS cnt FROM table WHERE DATE(datetime_field) BETWEEN 'YYYY-MM-DD_start_date' AND 'YYYY-MM-DD_end_date'

Link to comment
Share on other sites

Well yes this is one of those issues where you have a DATE time so when you specify a date by itself, it gets turned into the equivalent of midnight on that day.  You can rectify that by having the dates be '2011-05-02 23:59:59' or you can change those to be > and

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.