So what I want to do is basically this:
Month and Year:
-All the data sent on that month and year
For example:
February 2013:
-The posts on this date
March 2013:
-The posts on this date
and so on.
I have a date column on my table and it has the following format: day.month.year
I am using PHP and MySQL.
I have my own MVC framework that I'm using. I will simplify it to make it more understanble here. The function to fetch data:
function selectAll($sql){
$find = $this->prepare($sql);
$find->execute();
$fetchdata = $find->fetchAll(PDO::FETCH_ASSOC);
return $fetchdata;
}
And the code to get the dates :
$sql = "Select YEAR(Date) AS year, MONTH(Date) AS month, Headline from blog";
$data = $this->db->selectAll($sql);
foreach($data as $key => $value) {
$date[] = $value['month'].'.'.$value['year'];
}
$dateunique = array_unique($date);
This give me the results for the dates but I cannot figure out how to put the correct data under each date.
Convert the varchar date field into exact date using
str_to_date function
Using DATE_FORMAT you can select month wise record
Where $input is %Y-%m format // ex: 02-2013-> feb 2013
SELECT * FROM `table`
WHERE DATE_FORMAT(str_to_date(`date_column`, '%d/%m/%Y'), '%Y-%m')= '".$input."';
Use DATE_FORMAT mysql function.
Example
SELECT DATE_FORMAT(date_field,'%M') as Month,DATE_FORMAT(date_field,'%Y') as Year FROM table_name
and then debug result of query you will see two extra column for month and year then display it as your desire.
if date_field is varchar then use str_to_date mysql function.
SELECT STR_TO_DATE(date_field,'%M') as Month, STR_TO_DATE(date_field,'%Y') as Year FROM table_name;
OP question update
Try GROUP_CONCAT
SELECT
*,
GROUP_CONCAT(created) AS month
FROM
table_name
GROUP BY MONTH(created) DESC;
Related
In mysql how to get the sum between 2 dates.
In my query I'm getting the days between the same months(01-01-2016 to 31-01-2016) But I'm not getting 2 different months (01-01-2016 to 06-02-2016) the result is returns null value.
select sum(cash),sum(advnce)
from tablename where date
between '$strt_dte' and '$srch_prev_date'
GROUP by date
Give the following a go. Make sure your dates are formatted correctly!
$strt_dte = '2016-01-01';
$srch_prev_date = '2016-01-31';
$sql = "
SELECT SUM(cash), SUM(advnce)
FROM tablename
WHERE date BETWEEN '{$strt_dte}' AND '{$srch_prev_date}'
GROUP BY date
";
I have stored Date in database in dd-mm-yy format, for example 03-10-2013,
How to search record by month? Month in digit (01 to 12);
I am using currently
$query = "SELECT * FROM data WHERE date LIKE %$month%";
but this not working properly.
I am assuming when you say dates as stored in the database in a format, that they are not stored using a "date" type and instead are using a varchar or char type for the column.
Based on that there are few ways to do this.
Leave the database as it is and convert values on the fly.
SELECT * FROM data WHERE Month(STR_TO_DATE(datestrcolumn, '%d/%m/%Y')) = 5;
Change the type of the column to a "date" type column
SELECT * FROM data WHERE Month(realdatecolumn) = 5;
Change the type of the column to a "date" type column, store a separate column for the month.
UPDATE data set monthcolumn = Month(realdatetimecolumn)
then
SELECT * FROM data WHERE monthcolumn = 5;
Create an index on monthcolumn and this query will be much faster than the other queries if there is a lot of data
Fix the date format in your database structure first, change it to: yyyy-mm-dd
Then change your query statement to:
$query = "SELECT * FROM data WHERE MONTH(`date`) = '$month';
This will select the month as '5' or '11' or '12' which will give duplicates for differing years.
If you need the month with year (to avoid duplicate years):
$query = "SELECT * FROM data WHERE SUBSTR(DATE(`date`),1,7) = SUBSTR(DATE('$month'),1,7);
This will return: '2015-01' or '2014-12'
To get date as '01' or '04' or '12':
$query = "SELECT * FROM data WHERE SUBSTR(DATE(`date`),6,2) = SUBSTR(DATE('$month'),6,2);
Try this...
You could use MySQL MONTH() function
MySQL MONTH() returns the MONTH for the date within a range of 1 to 12 ( January to December). It Returns 0 when MONTH part for the date is 0
4 is april
SELECT * FROM tbl WHERE MONTH( date ) ='4'
You can do like it... as it is not in date format(YYYY-MM-DD)
$q="SELECT * FROM data WHERE MONTH(DATE_FORMAT(STR_TO_DATE(date, '%d-%m-%Y'), '%Y-%m-%d') ) = '$YOUR_SEARCH_MONTH' ";
I'm truly stumped on something - I have a table in my database with a column called 'today' that has Date and Time records. The column has entries that look like this:
October 25, 2014, 4:58 am
October 25, 2014, 4:36 am
I'm having trouble pulling the rows by date; I think the time stamp is messing with the MySQL query. And I need an SQL query to pull any records where the variable $today matches the date information in the column 'today'. This doesn't work:
$today = date("F j, Y"); // looks like this: October 25, 2014
$result = mysqli_query($link,"SELECT * FROM records WHERE today = $today"); // 'today' represents the column in the table
while($row = mysqli_fetch_array($result)) {
echo var_dump($row);
}
I just get an empty result, I think due to the time stamp. Can someone advise on a better MySQL query that will only grab the rows where $today matches the date in the 'today' column?
Although storing the date and time as string in varchar is not really a good idea, you could still alter your query to match string containing the current date with a LIKE statement:
$result = mysqli_query($link,"SELECT * FROM records WHERE today LIKE '$today%'");
That is just to get your current setup working as a temporary fix but i highly suggest you take a look at datetime and timestamp or similar date types if this is a serious project and not just playing around. with programming.
UPDATE
With a datetime you could get the dates which are the same as today with:
SELECT * FROM `records` WHERE `today` = CURDATE();
with a timestamp you would need to pass it as date so your query would be:
SELECT * FROM `records` WHERE date(`today`) = CURDATE();
You can just use the MySQL date functions:
SELECT *
FROM records
WHERE today = CURRENT_DATE;
If there is a time component on the today column, then the best structure is:
SELECT *
FROM records
WHERE today >= CURRENT_DATE and today < date_add(CURRENT_DATE, interval 1 day)
It's obvious that both dates are not equal. Both dates are treated like text values and are not equal. You need to convert the column containing date in your MySQL query as such:
$result = mysqli_query($link,"SELECT * FROM records WHERE DATE_FORMAT(today, '%F %j, %Y') = $today");
Note that you have to change your column to store values of the type of DATE. Or just use queries as proposed in other answers.
I want to read the single day, month and year, without adding 3 extra MySQL-rows, in this format (with PHP):
day: 01
month: Jan, Feb, Mar..(first three letters)
year: 2011
This is table and PHP script, which I use now:
I add the date with PHP:
mysql_query("INSERT INTO news (...,`time`) VALUES (...'".date(d.".".m.".".Y)."');");
I read it out with:
$query = "SELECT * FROM news";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {
echo $row['time'];
}
MySQL table:
news:
time(text):
"27.03.2011"
Query should be:
mysql_query("INSERT INTO news (...,`time`) VALUES (...'".date(d.".".M.".".Y)."');");
M instead of m gives you the 3 letter textual representation of the month.
Get it with:
while ($row = mysql_fetch_assoc ($result)) {
echo date( 'd', strtotime( str$row['time'] ) );
echo date( 'M', strtotime( str$row['time'] ) );
echo date( 'Y', strtotime( str$row['time'] ) );
}
Read more on:
http://php.net/manual/en/function.date.php
you can either do it in PHP, check the strftime function or use in SELECT something like
SELECT DAY(date) as day, MONTH(date) as month, YEAR(date) as year FROM table
and in php you would acccess it as $result['day']. $result['month'] etc. the "date" in SELECT query is of course the name of the column in which you store your date. I would recommend strftime
You can use MONTH(), DAY(), YEAR() Mysql functions., i.e,
SELECT MONTH(`time`) AS `month`, DAY(`time`) AS `day`, YEAR(`time`) AS `year` FROM `news` [...]
SELECT *, substring(1,2) as day, substring(4,2) as month, substring(7) as year FROM table
Also you can(and should) use date table format and use DAY(), MONTH(), YEAR() functions
You should be using a DATETIME column in your mysql table. MySQL is then responsible for storing the date in its own internal format, and you can retrieve it in any format you need.
To insert the current date, you can simply
INSERT INTO news (...,`time`) VALUES (...,NOW())
To retrieve it in the format you want, use
SELECT DATE_FORMAT(`time`, '%d.%b.%Y');
Documentation on MySQL DATE_FORMAT()
In mysql database i have this column called:
Name: Date
Type: datetime
I have few values in that column:
2009-01-05 01:23:35
2009-03-08 11:58:11
2009-07-06 10:09:03
How do I retrieve current date? I am using php.
in php:
<?php $today = date('Y-m-d');?>
How to write a mysql query to retrieve all today date data?
Should i change the column type to "date", then insert values like "2009-07-06" only, no time values???
You don't need to use PHP, MySQL has a function to get the current date:
SELECT field FROM table WHERE DATE(column) = CURDATE()
Documentation: CURDATE, DATE.
If your column is only ever going to need the date part and never the time, you should change your column type to DATE. If you insist on doing it through PHP, it is the same thing, really:
$today = date('Y-m-d');
$query = mysql_query("
SELECT field FROM table WHERE DATE(column) = '$today'
");
For date time it is not usefull, instead I try this and working...
Today's Visitors!
sql > select user_id from users where last_visit like concat('%' , CURDATE() , '%');
// last_visit coloumn of type 'datetime'