473,545 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ 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 5029
>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_wa nt
should be easy.

dayofweek(curre nt_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(curr ent_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(curr ent_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(cur rent_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_DA TE())
The current month?
WHERE MONTH(dateField ) = MONTH(CURRENT_D ATE())
Year?


WHERE YEAR(dateField) = YEAR(CURRENT_DA TE())

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
2228
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 records returned when I execute: $sql = "select * from tbl where ExpiryDate > " . date("Y-m-d"); mysql_query($sql);
4
2244
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 of the ranges into an array, containing the "IP ==> internalid" where internalid is the Primarykey of the table from which I can get
5
3476
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, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert...
0
1463
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 myself but now I want to move to textpattern (http://www.textpattern.com/). Using a mySQL tool called NaviCat, I can easily create queries regarding...
4
5390
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
2287
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 or after another date. It works, but I didn't realize until after I finished it that it won't work for dates outside the range of 1969-2033. I...
4
7546
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 another two sets of dates. At the moment I have done this select statement select * from table_name where
5
1335
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 apart then a warning message of some type with display saying that its been four or more days. I have my database made, and I've made a form for...
4
2414
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 I'm inserting a date using the MySQL function now(). So far so good. When I var_dump() the value from the database it says it's a string,
0
7468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7757
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.