php fetch date column from database - php

SELECT c.enddate FROM cohort c ORDER BY c.enddate DESC LIMIT 1
I have a sql query above, it works in database, the result select a date like: '2018-07-18'
I try to use while loop with mysqli_fetch_row in php to fetch this date, but the result will only fetch:
2018
How can I get the whole date?
if ($runquery = $conn->query($result_validation))
{
//get the enddate from the last cohort
while ($row = mysqli_fetch_row($runquery))
{
$lastDate = $row[0];
}
}
$row[0] only display first number: 2018.

Adding DATE() function in the column will convert it to a valid date format like for the example below!
SELECT DATE(c.enddate) FROM cohort c ORDER BY DATE(c.enddate) DESC LIMIT 1

Try SELECT DATE(c.enddate) FROM cohort c ORDER BY c.enddate DESC LIMIT 1,

Try this one
SELECT DATE_FORMAT(c.enddate,"%Y-%m-%d") FROM cohort c ORDER BY c.enddate DESC LIMIT 1
The DATE_FORMAT() function formats a date as specified by a format mask.

Related

select records from sql database order by date DESC, but I want show records in table in date ASC

I have 7 records in a table but I want select only last 5 records sorting by date DESC. And I want to show these records in my table sorting by date ASC.
My Code:
$select_record = mysqli_query($con, "select * from table order by date DESC limit 5");
while($row=mysqli_fetch_array($select_record)){
$date = $row['date'];
echo "<tr><td>$date<br></td></tr>";
Records in My Table like this
Date
2014-05-15
2014-04-15
2014-06-15
2014-02-15
2014-07-15
2014-01-15
2014-03-15
My code give me result like this
Date
2014-07-15
2014-06-15
2014-05-15
2014-04-15
2014-03-15
But I want result like this
Date
2014-03-15
2014-04-15
2014-05-15
2014-06-15
2014-07-15
Do second SELECT on top of first one:
SELECT t.* FROM
(SELECT * FROM `table` ORDER BY `date` DESC LIMIT 5) t
ORDER BY t.`date` ASC
short version:
(SELECT * FROM `table` ORDER BY `date` DESC LIMIT 5) ORDER BY `date` ASC
Edit your while loop:
Instead of doing this:
while($row=mysqli_fetch_array($select_record)){
$date = $row['date'];
echo "<tr><td>$date<br></td></tr>";
}
Try doing this:
create an array that can store your date values retrieved from query
while($row=mysqli_fetch_array($select_record)){
$date = $row['date'];
Add $date to the array you created
}
Iterate the array from the END to START and print dates .
{
echo "<tr><td>$date<br></td></tr>";
}
Store Your dates in Array and then use array_reverse() PHP Function to get the result in reverse order.
array_reverse — Return an array with elements in reverse order
Reverse array values while keeping keys
Try this:
$select_record = mysqli_query($con, "select * from table order by date DESC limit 5");
// Sort in chronological order.
usort($select_record, function($a, $b) {
return strcmp($a['db'], $b['db']);
});
while($row=mysqli_fetch_array($select_record)){
$date = $row['date'];
echo "<tr><td>$date<br></td></tr>";
The simple solution would be to extract the rows and the reverse the list in PHP land. This would be my solution in your simple case based on the result size.
If you really want to do it without having to store the full result in PHP land you could do it with a sub-select. This would mean replacing your query expression with:
$select_record = mysqli_query($con, "SELECT * FROM (select * from table order by date DESC limit 5) AS result ORDER BY date");
But I would think this to be less maintainable than just reversing the result in PHP.
You can also save your result in array. Then you can use sort($array);
PHP function Sort

Fetch rows from mysql table if a condition satisfies in php

I have a table like this
my table is here
I need to fetch the table such that the valid_from date is less than the the date which I have(date cannot be current date).
For example. If my date is 02-04-2015, I should get the row with id 120.
Plz help me to do this in php
Please try below quewry :
$input_date = "02-04-2015";
$your_date = date("Y-m-d",strtotime($input_date));
$sql = "SELECT * FROM `table_name` where DATE(validFrom) < '".$your_date."'";
as per your example
SELECT * FROM `your_table_name` where validFrom < '02-04-2015' order by validFrom desc LIMIT 1
or
SELECT * FROM `your_table_name` where validFrom < '02-04-2015' order by validFrom asc LIMIT 1
set order by as per your expectation of output
here you want to pass date as per your date format

Select max and min values in one sql query

i have a table that has records with its timestamp. now i have a requirement to get the oldest record and the newest record
9/10/2014 6:54
9/10/2014 6:53
9/10/2014 6:51
9/8/2014 8:09
9/8/2014 7:00
9/5/2014 7:38
9/5/2014 3:57
9/5/2014 3:51
9/4/2014 11:09
9/4/2014 8:39
currently this is how i obtain them by sending two database calls which slows downs processing
$new_timestamp = mysql_query("SELECT TIMESTAMP FROM $rectable ORDER BY TIMESTAMP DESC LIMIT 1");
$col_old = mysql_fetch_assoc($new_timestamp);
$old = $col_new['TIMESTAMP'];
$new_timestamp1 = mysql_query("SELECT TIMESTAMP FROM $rectable ORDER BY TIMESTAMP ASC LIMIT 1");
$col_new = mysql_fetch_assoc($new_timestamp1);
$new = $col_new['TIMESTAMP'];
is there any way to optimize this code and fullfiill requirement without sending two database calls, througha special query or a stored proceedure
You can use max and min to get the newest and oldest timestamps
select max(timestamp), min(timestamp) from mytable
Try with UNION
select TIMESTAMP as old FROM $rectracktable ORDER BY TIMESTAMP DESC LIMIT 1
union all
select TIMESTAMP as new FROM $rectable ORDER BY TIMESTAMP ASC LIMIT 1

SQL selecting records with dates before today

I have a mysql DB with tables, of which in the one table I have a date type field, I want the most recently passed date - so I want it to order by dates descending, but only take records from before today, and then take only the top most one using the LIMIT function, and also there is the addition of the WHERE clause being that the offer must be for the selected city.
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]."
ORDER BY exp_date DESC
LIMIT 0, 1");
ADD another condition to where clause
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]." and Date < CURRENT_DATE()
ORDER BY exp_date DESC
LIMIT 1");
SELECT * FROM deals WHERE city = 2 AND exp_date < CURDATE()
ORDER BY exp_date DESC LIMIT 0, 1
Add the following condition to Where:
... and exp_date < CURDATE()
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.

How can I order by date?

I have a problem with a query (mysql).
I have to order by date ASC tha problem is that I also have empty date in the table. Mysql set them to 0000-00-00, so when I order by date those dates are shown first, but i need to put the "empty dates" at the end, how can I do?
Example:
0000-00-00
2011-01-01
2010-12-12
Query: .... ORDER BY date ASC
Results should be:
2010-12-12
2011-01-01
0000-00-00
Thanks
ORDER BY IF(date = '0000-00-00', 1, 0) ASC, date ASC
Assuming you're doing:
SELECT *
FROM table
ORDER BY date ASC
you could do
SELECT *, IF(date > 0, 1, 0) AS has_date
FROM table
ORDER BY has_date ASC, date ASC
I believe that would do it for you.

Categories