Get data from the current month - php

I have a question. So I need to get data from the last days of months using a sql request. For example :
Now date is 22/08/2016, I need to get data from 01/08/2016-22/08/2016;
Now date is 04/06/2016, I need to get data from 01/06/2016-04/06/2016;
Sorry but I dont have an idea. Thx in advance and sorry for my english

I would do something like this:
SELECT <table-columns> FROM <table-name>
WHERE (<date-column> BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
This DATE_FORMAT(NOW() ,'%Y-%m-01') will return the first date of your current month and year.

Need to use something like:
YEAR(time) = YEAR(NOW())
AND MONTH(time) = MONTH(NOW())

You are looking for something like this:
SELECT * FROM tbl_name WHERE yourDate.MONTH()=NOW().MONTH();
Refine the syntax from here.

You need to get the first day of the month for a given date then, in order to make your comparison with the database:
select date(concat_ws('-',year(curdate()),lpad(month(curdate()),2,'00'),'01')); gives you that (for curdate() here)

Related

Match a date to a time date format in php

I am trying to get records from my database which are added on a specific date, for example records added on the 2013-09-05. However the records are stored in my database in datetime format so 2013-09-06 08:22:35
I have tried to use a like format thinking it might just match the first half but that makes no difference.
SELECT search_term, COUNT(search_term) as count FROM $tableName WHERE client_id = '{$client_id}' AND timedate LIKE '$dates' ORDER BY count DESC LIMIT $start, $limit
I would be grateful for suggestions or a nudge in the right direction.
Thanks
you could use DATE() to get date part from datetime, so change:
...AND timedate LIKE '$dates'...
to
...AND DATE(timedate) = '$dates'...
or you could also do:
...AND timedate LIKE '$dates%'...
You can convert date using DATE_FORMAT
Use in Query as below,
AND DATE_FORMAT(timedate, '%Y-%m-%d' ) = '$dates'
Document

What mysql code do I need to use? to find all ids made between date

I'm trying to work out the amount of ids between two dates basically I want to find out how many users I have gotten today so im not sure what I need for it really :/
I have a column call date which has the users date of registration
And I have the ID column of course which will help
Please help
Use BETWEEN. For example:
SELECT
COUNT(*) AS total_users
FROM
users
WHERE
date_of_registration BETWEEN CURRENT_DATE AND INTERVAL CURRENT_DATE - 1 WEEK
SELECT
id
FROM
t1
WHERE
DATE(`date`) = CURDATE()
The DATE() call is onlyi necessary of date is a timestamp.

What data type to use for days of week (i.e. Sunday, Monday etc) in MySQL?

I need week days in my tables and I need a query to compare with today.
Something like this:
$query = "select course_ID from course where day = (today)";
This is what I want (day = (today)";) and I need the data type for storing week days in tables.
Use the DAYOFWEEK function to map from a date to the day of the week.
Maybe something along the lines of:
$query = "select course_ID from course where dayofweek(some_field) = dayofweek(now())";
SELECT course_ID FROM course WHERE DATE_FORMAT(date_field_name, '%Y-%m-%d') = CURDATE()
This will do the work no matter if the day is the same year or not.
If you want to match against weekday
SELECT course_ID FROM course WHERE DATE_FORMAT(date_field_name, '%W') = DATE_FORMAT(NOW(), '%W');
Last edit
To match data in day_column if the values are Monday ...Sunday
SELECT course_ID FROM course WHERE day_column = DATE_FORMAT(NOW(), '%W');
You can use an ENUM datatype and have it predefined with the 7 days. See here for more information : http://dev.mysql.com/doc/refman/5.0/en/enum.html
Since dayoftheweek ( as suggested by #borrible) returns numbers, it will be convenient to use this function to compare dates and the days you stored.
I suggested you store the days in numeric data type such as TINYINT.

want to fetch only the month part from a date in mysql

Is it possible to fetch only the month part from a sql date entry?
like the date is stored like '2011-01-06'. if i want to fetch only the '01' portion how can I do that?
thanxx in advance.
You can use DATE_FORMAT to do it, like this:
SELECT DATE_FORMAT(DateField, '%m') FROM table
you can do it in mysql
like :
select DATE(date_field) from mytable
from manual :
MONTH(date)
Returns the month for date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero month part.
mysql> SELECT MONTH('2008-02-03');
-> 2
in a mysql query, you can just do this:
MONTH(date_field)
mysql> SELECT MONTH('2008-02-03');
Result: 2
You can use the MONTH function of MySQL:
SELECT MONTH('2011-01-06');
Result:
1
To get month name instead, you can use MONTHNAME like this:
SELECT MONTHNAME('2011-01-06');
Result:
January
You may want to try
select substring(`mydate_field`,6,2) from `table_name`;
In ANSI-SQL you can use EXTRACT:
SELECT
EXTRACT('month' FROM fieldname)
FROM
tablename;
This works in many databases, MySQL as well.

Select Before The Date

I have a date, say its called $date. I want a a mysql_query to search a select number of weeks,days or even months before my $date. Is this possible? My explanation is not the greatest, but I do need a answer for this and do not know how to properly question it.
You could use mysql interval function?
"select * from table where `date` BETWEEN DATE_SUB(".$date.",INTERVAL 15 DAY ) AND CURDATE( )
That'll return the records from the last 15 days, you could use = insted of between if you want the records exactly 15 days old, or modify it for days, months, etc.
edit: if your working with php's time() remeber to use FROM_UNIXTIME($phpdate) inside your query.
i have a solution for this in SQL, Take it, if it would helps you
Day($date) gives you the date in the vaariable
Month($date) gives you the Month in the vaariable
Year($date) gives you the year in the vaariable
using simple where conditions, now you can search for a particulars
You can use the DATE_ADD and DATE_SUB functions to modify a date, and mysql understands a BETWEEN clause using dates. However, you can also use the TIMESTAMPDIFF function like so:
"SELECT foo FROM table WHERE TIMESTAMPDIFF(DAY, dateField, '$date') < '$desired_days'"

Categories