473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Querying dates in mysql

Hello,

MYsql and PHP

If I want to extract data with todays date then it's 'where blab blah =
current_date()'

For yesterday it's 'where blah blah = current_date()-1'

But can anyone tell me how to extract all data from the start of the
current week? Is there any way in Mysql to work out the start of the
current week?

The current month?

Year?

Thanks,

rg

www.pocketpcheaven.com
Sep 13 '05 #1
2 5024
>MYsql and PHP

If I want to extract data with todays date then it's 'where blab blah =
current_date()'

For yesterday it's 'where blah blah = current_date()-1'
I have serious doubts about the above working on the first day
of the month, especially if you use numbers larger than one.
For example, current_date()-30 appears to be August 83, 2005.

adddate() and subdate() are very useful functions.
But can anyone tell me how to extract all data from the start of the
current week? Is there any way in Mysql to work out the start of the
current week?
Given that you've got the date you need, extracting data
WHERE datestamp >= the_date_you_want
should be easy.

dayofweek(current_date())
is the current day of the week (1 = Sunday ... 7 = Saturday).
Now take the day number of whatever you think is the first day of
the week (there is *NOT* a universal consensus on this) and subtract.
Assuming you think Wednesday is the first day of the week (why not?),
that's 4. As is usual with day-of-week calculations, you need to
mod by 7 for wraparound.

The first day of the week (a Wednesday) is:
subdate(current_date(), INTERVAL (dayofweek(current_date()) - 4 + 7)%7 DAY)
The current month?
If today is the Nth day of something, we go back N-1 days to get
to the first.

subdate(current_date(), INTERVAL dayofmonth(current_date()) - 1 DAY)
Year?


This one I think I'll cheat on with the representation: get
the year and tack on January 1.

concat(year(current_date()), '-01-01')

Gordon L. Burditt
Sep 13 '05 #2
toedipper wrote:
But can anyone tell me how to extract all data from the start of the
current week? Is there any way in Mysql to work out the start of the
current week?
If you need all dates in the current week, you could just compare the weeks:

WHERE WEEK(dateField) = WEEK(CURRENT_DATE())
The current month?
WHERE MONTH(dateField) = MONTH(CURRENT_DATE())
Year?


WHERE YEAR(dateField) = YEAR(CURRENT_DATE())

For more information on using these functions, see:
http://dev.mysql.com/doc/mysql/en/da...functions.html

If you want to get only those dates from the beginning of the time
period to the current date, excluding dates after the current date, you
could append the following term to each of the above examples:

AND dateField <= CURRENT_DATE()

Regards,
Bill K.
Sep 14 '05 #3

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

Similar topics

3
by: Mark Stevens | last post by:
I am trying to do select all of the records in a table where they data has not expired. The table contains two fields Subject (varchar(100)) and ExpiryDate (Date). I am getting all of the...
4
by: Michael Whittaker | last post by:
Hello! I have a problem with my php script. The script's task is to search an IP-Database with ranges as entries and find, in which range the entered IP is. OK, I've queried the MySQL-Results...
5
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql,...
0
by: Massimo Fiorentino | last post by:
Hello there! I am a bit of a newbee into the mySQL world and I have a question regarding switching from one DB to another. I have for a couple of years used a very simple CMS-system created by...
4
by: Jorey Bump | last post by:
I can retrieve today's date: mysql> SELECT CURDATE() AS begin; +------------+ | begin | +------------+ | 2005-06-01 | +------------+ 1 row in set (0.00 sec)
5
by: DJ Craig | last post by:
I have written a program that uses the built-in PHP date functions. All that the program does is calculate the amout of time between two dates, or calculate the date a certain amount of time before...
4
by: minapatel | last post by:
Probably a simple solution but can't find it myself can someone help????? I would like to extract data from a table where the dates are between two dates and then loop to select more data for...
5
by: Dollarstar | last post by:
I'm not sure if I've disribed this right but this is what I need: I have two fields with dates in. I need some code that will look at the two dates and if the dates are more than four days...
4
by: Michael Sharman | last post by:
Hi guys, I'm a little confused with dates. Ok, all I want to do is store a date in MySQL as a datetime object and be able to read and format it using PHP. My datatype in MySQL is DATETIME and...
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.