PHP: date between date - php

I have two dates:
10-11-2010 and 17-11-2010
Now i would like to SELECT all rows with the dates between those two.
How can I do that?

its very simple using between in where clause
, read more
select * from mytable where date between '10-11-2010' and '17-11-2010'

Sounds like a SQL question. Try the between condition.

$datefrom=date('Y-m-d');
$dateto=date('Y-m-d');
$sql = 'select * from mytable where depositdate between $datefrom and $dateto';

Related

Sort by newest date using php

I have the following date field, I need to sort by newest date.
Please help me to solve this.
tried the following query but it's not getting the correct output.
17/12/2014
26/01/2016
19/11/2014
30/06/2014
I need to sort in the following format :
26/01/2016
17/12/2014
19/11/2014
30/06/2014
Here is my code.
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column name,'%m/%d/%Y')";
your code is not working because you have dd/mm/yyyy format. so you need first date then month in conversation
$queryold="SELECT * FROM tablename order by STR_TO_DATE(column_name,'%d/%m/%Y')";
If your column's type is 'datetime' you just have to run this query:
$query = "SELECT * FROM tablename ORDER BY datecolumn DESC";
If it's a varchar the good query is:
$query = "SELECT * FROM tablename ORDER BY CONVERT(datetime, datecolumn) DESC";

comparing two fields in mysql

is their any query to pick certain date between two time stamps?The Problem is these two times stamps are set in two diffrent fields
SELECT * FROM table WHERE fs_from > theDate AND fs_to < theDate;
I suggest to use prepared statements and insert a date object into the query.
In my opinion you can try in this way (If I really understood your question)
SELECT myFields FROM myTable
WHERE myCertainDate BETWEEN fs_from AND fs_to
Try this:
SELECT *
FROM table
WHERE fs_from > 'some date'
AND fs_to < 'some other date'
Which date do you want? You can choose a random date by doing:
select t.*,
date_add(t.fs_from, interval cast(datediff(fs_to, fs_from) * rand() as int) day
) as ArbitraryDateBetweenTwoDates
from table t

ORDER BY two dates mysql query

I am going to give order in query like:
SELECT * FROM `sales_report` ORDER BY `cancel_date`, `pur_date`
But its not working could you please check what is wrong in this query because its not ordering dates?
pur_date type is date and cancel_date type is text so is it issue ? I can't change its type.
I echo a different think like:
if(cancel_date != ''){
$transection_date = cancel_date;
}
else {
$transection_date = pur_date;
}
Try to use STR_TO_DATE() function:
SELECT
*
FROM
`sales_report`
ORDER BY
STR_TO_DATE(`cancel_date`, '%Y-%m-%d'), `pur_date`;
As zerkms mentioned in comments: You should use format specifiers, dependent of your text values in cancel_date column.
SELECT *, CAST(`cancel_date` as DateTime) cancelDate
FROM `sales_report`
ORDER BY `cancelDate`, `purDate`
This should work

php select query with different date formats

I have a variable called $date with a date format like 2012-01-30. On the other hand I have a field called srt_date_sortie in a mysql table. The format of that field is like 2012-01-30 11:31:00. I would like to select all records where $date and srt_date_sortie have equal dates not taking into consideration the time. Hope someone can help.
mysql_query("SELECT * FROM sorties WHERE srt_date_sortie = '$date'");
Here below how I build $date :
++$input;
$test=$input." days";
$date=date('Y-m-d', strtotime($test));
Thank you in advance for your replies. Cheers. Marc
For performance reasons you should avoid comparing against a derived column like DATE(srt_date_sortie) as this will prevent any indexes from being used. This negates a few of the solutions already posted.
The most efficient way of querying against your DATETIME field is by using a range query like:
mysql_query("SELECT * FROM sorties WHERE srt_date_sortie BETWEEN '$date 00:00:00' AND '$date 23:59:59'");
Unless you need all of the rows from the table the SELECT * could be inefficient, just list the rows that you need to retrieve.
Change your query with BETWEEN keyword for date start time and end time.
You can format date using DATE_FORMAT in mysql
mysql_query("SELECT * FROM sorties
WHERE DATE_FORMAT(srt_date_sortie,'%Y-%m-%d') = '$date'");
mysql_query("SELECT * FROM sorties WHERE DATE(srt_date_sortie) = '$date'");
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date
mysql_query("SELECT * FROM sorties WHERE srt_date_sortie LIKE '%2012-01-30%'");
$date=date('Y-m-d H:i:s', strtotime($test));
Use it
Check the TIMESTAMPDIFF mysql function
mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-> 128885

getting current (system) day using an SQL query in PHP

I have this MySQl database that has a table which contains a column that contains days of the week like "Mon", "Tue" etc.
How do I get query the database and match this column content with the current (system) day?
like say
select .... where tablename.columnname = systemday
thanks.
Try using date_format(now(), "%a") as your condition value.
Read more here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Haven't you tried searching ?
Anyways i hope you want something like this,
$cur_day= date('D');
select .... where tablename.columnname = $cur_day;
WHERE tablename.columnname = substr(dayname(now()),1,3)
or
WHERE tablename.columnname = date_format(now(),'%a')
Note, however, that it may be affected by locale settings.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekday
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_dayofweek
Returns the weekday index for date (1 = Sunday, 2 = Monday, …, 7 = Saturday). These index values correspond to the ODBC standard.
Example:
SELECT * FROM mytable WHERE dayOfTheWeek=WEEKDAY(NOW())
Also, you can do this using PHP`s strftime and date functions:
$query1 = mysqli_query('SELECT * FROM mytable WHERE dayOfTheWeek="'.date('N').'"');
$query2 = mysqli_query('SELECT * FROM mytable WHERE dayOfTheWeek="'.strftime('%u').'"');
But, I think, it is better to generate this in PHP-only way.
<?php
$day=date('D');
$sql='select * from the_table where the_table.'.$day.'=systemday';
?>

Categories