473,387 Members | 1,904 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.

Selecting between dates with different timestamps

I'm in a bit of a mess here, trying to deal with some timestamp
issues.

I need to select items from a database with a datetime timestamp:

0000-00-00 00:00:00

I need to select these items based on several different criteria, for
which I will use separate select statements, but some of them are a
bit confusing.

I need to select items that are less than 24 hours old, between 24 and
48 hours old, between 3 to 5 days olf, between 6 to 10 days old,
between 11 to 30 days old, and items older than 30 days.

In the past when comparing times I have always converted the database
timestamp to Unix time, set a nowTime variable to current time in unix
time, and done the addition and subtraction. In this case, that
doesn't necessarily work, as I need to use "BETWEEN" in the
timestamps, and I'd really rather handle the separation of times with
the select statement than in the code.

Can anyone point me in the right direction, I'm not sure what the
select statement should look like.
Thanks,
fnord
Jul 20 '05 #1
1 2960
fnord wrote:
I need to select items that are less than 24 hours old, between 24 and
48 hours old, between 3 to 5 days olf, between 6 to 10 days old,
between 11 to 30 days old, and items older than 30 days.


All in one query, or one criteria for each query? Perhaps something like
this:

select *
from devices
where date_ between
date_sub( now(), INTERVAL 30 DAY )
and date_sub( now(), INTERVAL 11 DAY );

Instead of "between", you can also use normal > < = comparison
characters, like this:

select *
from devices
where
date_ >= date_sub( now(), INTERVAL 30 DAY )
and date_ <= date_sub( now(), INTERVAL 11 DAY );

After INTERVAL you give the number and after that DAY, HOUR, SECOND,
etc. Read full list of choises from DATE_SUB() description from:

http://dev.mysql.com/doc/mysql/en/Da...functions.html
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Sims | last post by:
Hi, I have a field in my DB that saves the date/time as a integer. I get the time on my server using the time() function. now, moving away from php and looking at (My)SQL only. If I use...
3
by: jrc4728 | last post by:
I have a MySQL table with the date stored in three fields as string values like this. (sorry, its imported data) str_yy str_dd str_mm ------------------------ 05 01 04 05 ...
8
by: Angelos | last post by:
What do you think is the best way to store Dates into a database ? If you want to keep logs or buckups.... I am using date('dmYHis') but I doesn't work really well ... Is it better to use...
1
by: fnord | last post by:
I'm in a bit of a mess here, trying to deal with some timestamp issues. I need to select items from a database with a datetime timestamp: 0000-00-00 00:00:00 I need to select these items...
10
by: Craig Wahlmeier | last post by:
Am I the only one in the world that selects dates and timestamps with OLEDB? V8 of UDB has brought me a big problem. The PATCH2=24 setting no longer works the way it used to. The settings of...
2
by: p175 | last post by:
People, I have an ESE 8.2.2 database running on win2k server . I need to do some system testing that requires I reset the dates back a couple of years then progressivily move it forward to...
3
by: MaRCeLO PeReiRA | last post by:
Hi Guys, I am in troubles with some dates. "I need to know the difference, in days, between two dates." Well, if the difference is less than a month, so I could use:
9
by: Bosconian | last post by:
I must be having a brain freeze because I can't figure out how to display dates older than 1970. I have birth dates stored in MySQL as "date" data types (exp. 1955-04-06). I'd like to display as...
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.