I am making a blog, and I want to be able to filter results based upon the date the post was added. The posts have a datetime in the format 2013-11-15 04:08:03. Now I want to click on a list of months, so let's say I click November it should show all posts in November, but I'm not to sure how I will extract the month from the above time and then add that to a query.
What I've got go so far is a list of months. You select a month that pass's on the numeric value of the month, that is, January = 01. Now that all works, and I can echo out that it works on the net page, but how do I link that with Y/m/d so that the 01 selects all entries with the 01 month?
I was thinking it would be something like select all where m = the selected month (m being the m in Y/m/d).
To get all months, in which you have your posts, you can run this simple query:
SELECT
DATE_FORMAT(`date_field`, '%Y%m') AS `id`,
DATE_FORMAT(`date_field`, '%M %Y') AS `title`
FROM
`posts_table`
GROUP BY
DATE_FORMAT(`date_field`, '%Y%m')
ORDER BY
`date_field` DESC
When you fetch this year-months, create links with id fetched from previous query, like:
echo '' . $row['title'] . '';
# November 2013
And when user lands on this link, fetch GET parameter and select all posts for this month:
SELECT *
FROM `posts_table`
WHERE DATE_FORMAT(`date_field`, '%Y%m') = ?
Where ? should be selected month, like 201311. But if you have some index on date_field, then you should use BETWEEN so index will be applied:
$selected = '201311';
$from = new DateTime("{$selected}01");
$sql = "
SELECT *
FROM `posts_table`
WHERE `date_field` BETWEEN ? AND ?
";
// first ? should be $from->format('Y-m-d 00:00:00');
// second ? should be $from->format('Y-m-t 23:59:59');
Related
I have a MySQL DB where I store dates in the following format
2017-04-03
I need to split or search dates to get all dates and records for January, Feb, March and so on in between all the dates I have in DB
$result = mysql_query("SELECT * FROM lbs_trace_etrack WHERE MONTH(lbs_date) = MONTH(CURDATE()) AND YEAR(lbs_date) = YEAR(CURDATE()) ORDER BY lbs_date DESC, lbs_time DESC");
I use the above query to search Current month and year. I am drawing up a graph that shows me stats from each month this is the reason I want each months count
I need to place the counts for the search in the following format.
var seriesData = [{
name: 'Hijackings',
data: [Value Jan, Value Feb, Value March, and all the other months ]
}, {
If anyone can just help me with the filter on each month query would help me greatly
Try this query:
select count(*) as total, MONTH(lbs_date) as track_month, YEAR(lbs_date) as track_year
FROM lbs_trace_stack t
GROUP BY track_year, track_month
Or if you only want month or only want year, you have just to remove
MONTH(lbs_date) and track_month --> if you want to see the year remove this
YEAR(lbs_date) and track_year --> if you want to see the month remove this
from the select part and group by part.
In addiction, if you want to filter on one or more months you can of course use the where statement, to set up your filter.
There are a several ways to write some where condition that do the same things, for example:
SELECT COUNT(*) as total, MONTH(lbs_date) as track_month
FROM lbs_trace_stack t
-- WHERE track_month = 2 -> February
-- WHERE track_month > 2 -> Form March
-- WHERE track_month = 2 OR track_month = 3 -> February or March
-- WHERE MONTHNAME(lbs_date)='February' --> if you want to use month name
GROUP BY track_month
I have found that this query works
$query = "SELECT rep_date FROM `bureau` WHERE MONTH(rep_date) = 1 AND type = 'Overdue'";
$result = mysql_query($query);
echo " ".mysql_num_rows($result)." "
?>
<?php
while($rows=mysql_fetch_array($result)){
?>
Using the MONTH(rep_date)=1 will filter all January changing 1 to 2 will do Feb and so on
I have 2 columns one having month and another year , I want to get all the records greater than that month and year
I cannot use date functions as those columns are of integer type, It gives incorrect results
How can I get records greater than that month and year
For example How can I get events which took place after february 2017
What about this approach:
SELECT * FROM table WHERE CONCAT(year, month) > 201702
WHERE CONCAT(year, month) > 201702
probably you have also some php code inside so:
$date = strtotime('2018-01-01 01:01:01');
$sql = 'SELECT * FROM YOUR_TABLE
WHERE CONCAT(year, month) > ' . date('Ym', $date);
SELECT * FROM events WHERE CONCAT(year,month) >= 201702
or without CONCAT
SELECT * FROM events WHERE( month > 2 AND year =2017) OR (year > 2017)
I want to have a SQL Query that selects between two dates of months.
Lets say between the 15th and the 15th of the next month.
This is for accounting purposes
I know with each month having different days adding 30 days or 31 days to the start date will not work especially in Feb. My accounting run is from the 15th of the month to the 15th of the next month.
My current query for selected dates are as follows
SELECT * FROM lbs_trace_etrack WHERE lbs_date >= '" . $dates . "'
AND lbs_date <= '" . $datee . "' AND lbs_client='$slcustom1' ORDER BY lbs_date DESC, lbs_time DESC
lbs_date start should be the 15th of each month and lbs_date end should be the 15th of the next month
By defining the dates as follow works but it is giving me from the November date to now and I want it from the 15th of this month seeing we have past the 15th of the current month already
$first = date('Y-m-15', strtotime("$last -1 month"));
$last = date('Y-m-t');
and the query
SELECT * FROM lbs_trace_etrack WHERE lbs_date >= '" .
$first . "'
AND lbs_date <= '" . $last . "' ORDER BY lbs_date DESC
Basically as we pass the current month 15th I need it to show me the current usage from the current 15th not the previous month 15th to now
If you really want what I understand you want,
SELECT l.*
FROM lbs_trace_etrack l
JOIN (SELECT s.cd, CASE
WHEN s.cd >= DATE(DATE_FORMAT(s.cd,'%Y-%m-15'))
THEN DATE(DATE_FORMAT(s.cd,'%Y-%m-15'))
ELSE DATE_SUB(DATE(DATE_FORMAT(s.cd,'%Y-%m-15')), INTERVAL 1 MONTH)
AS start_date
FROM (SELECT CURDATE() cd) s) d
ON (TRUE)
WHERE l.lbs_date >= d.start_date
AND l.lbs_date < d.cd
AND l.lbs_client = '$slcustom1'
ORDER BY l.lbs_date DESC, l.lbs_time DESC;
Please note that you still have the PHP variable $slcustom1 there.
The JOIN with subselect is motivated by the need to call CURDATE() only once, to avoid the rare occasion in which the date might change between two subsequent invocations.
Please check if comparisons <, >= etc. are what you want.
Edit:
I’ve moved the computation of start_date to the joined table. Even if in this way I have to call DATE(DATE_FORMAT()) three times, it is better than to compute DATE_SUB for every row, in the case we are in the first half of the month.
Could you able to use mysql day() function for your requirement.
Ex:
SELECT day("2016.12.15");
result:
15
Like wise you can use this function to check whether its "15"
select day(currentDate())
use DAY() function to get specific date
select * from My_Table where day(str_to_date(`Date`,'%d-%m-%Y')) = 1 or day(str_to_date(`Date`,'%d-%m-%Y')) = 15;
select group_concat(year(now()),"-",month(now()),"-",15)
I am interested in collecting data from the last month in the database with MySQLi.
But using INTERVAL 1 MONTH each month overlaps the other, so is there a way to tell the function to check my current month, start from the first day, and collect all data from said month?
Something like:
SELECT count(DISTINCT ip) AS visitor_ip FROM visitor_list
WHERE visited_date > ( INTERVAL CURRENT_MONTH_BEGINNING)
You can just use the current year and month, and 01 as the day:
SELECT count(DISTINCT ip) AS visitor_ip
FROM visitor_list
WHERE visited_date > CONCAT(DATE_FORMAT(NOW(),'%Y%m'), '01')
This has the advantage that any index on visited_date will still be used correctly.
First you need to generate current month starting date from PHP like this:
$monthStartDate = date('Y-m').'-01';
Then your query will become:
$sql = "SELECT count(DISTINCT ip) AS visitor_ip FROM visitor_list WHERE visited_date > DATE_SUB(now(), interval (datediff(now(), '".$monthStartDate."')) day)";
Hi everybody) I have table and few rows. How I can print all results for a specific date? Specific date like this - 30.01.2013 . Date previously saved in $date (in variable).
Google helped me), but it for last month..:
select id from tab where date_format(real_time, '%Y%m') = date_format(date_add(now(), interval -1 month), '%Y%m');
The date save in row TIMESTAMP.
select ...
from ...
where real_time = '2013-01-30'