How can i show Data from mysql specific month - php

$query = mysql_query(
"SELECT id, BuyerName,BuyerEmail,TransactionID,DateTime FROM `order`
WHERE DateTime="2015 AND 08""; LIMIT $start, $per_page"
)
or die(mysql_error());
I'm trying to make the query to show specific month and year.

You can use MySQL's Built-in YEAR and MONTH functions like:
SELECT `id`, `BuyerName`,`BuyerEmail`,`TransactionID`,`DateTime`
FROM `order` WHERE YEAR(DATE(`DateTime`))=2015 AND MONTH(DATE(`DateTime`)) = 8

If your column has a datatype of date you can use a range of dates for your criteria in a Sargable way
SELECT id, BuyerName,BuyerEmail,TransactionID,DateTime
FROM `order`
WHERE DateTime >='01-08-2015'
AND DateTime <= '31-08-2015'
For datetime you can write it as
SELECT id, BuyerName,BuyerEmail,TransactionID,DateTime
FROM `order`
WHERE DateTime >='01-08-2015 00:00:00'
AND DateTime <= '31-08-2015 23:59:59'

if type of column is datetime then try this
$query = mysql_query("
SELECT id, BuyerName, BuyerEmail, TransactionID, `DateTime`
FROM `order`
WHERE YEAR(DATE(`DateTime`)) = 2015 AND MONTH(DATE(`DateTime`)) = 8
LIMIT $start, $per_page
") or die(mysql_error());
or simply
$query = mysql_query("
SELECT id, BuyerName, BuyerEmail, TransactionID, `DateTime`
FROM `order`
WHERE `DateTime` LIKE '2015-08-%'
LIMIT $start, $per_page
") or die(mysql_error());

$time_you_want_to_choose = strtotime('previous month');
$ym = date('Y-m', $time_you_want_to_choose);
$query = mysql_query("
SELECT `id`, `BuyerName`, `BuyerEmail` , `TransactionID`, `DateTime`
FROM `order`
WHERE `DateTime` like '$ym%'
LIMIT $start, $per_page
");

Related

MySQL/PHP - Display Recent Date

I was able to apply this line onto phpMyAdmin and it worked just fine.
SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1
The problem is that when I added the rest of the code, the recent date shows up blank on the webpage. Am I missing something in this code?
<?php
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
echo "$date";
?>
Any help is appreciated. Thank you.
. Try this
$query = "SELECT id, date_format(`date`, '%m.%d.%Y') as `date` FROM TABLE ORDER BY. date DESC LIMIT 1";
$result = mysql_query($query);
$r = mysql_fetch_assoc($result);
$date = $r['date'];
echo "$date";
You didn't set $date variable. You need to use mysql_fetch_array function for your $result variable.
Ex:
`
$query = "SELECT id, date_format('date', '%m.%d.%Y') as 'date' FROM TABLE ORDER BY date DESC LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row); }
`

Previous record in MySql

In my table i have date records like 02-04-2016 , 03-01-2016 and 04-01-2016 if i am on 03-01-2016 i want the previous record which is 02-01-2016 But it gives me 01-01-2016 which is the first record of my table. No matter what date i am on.
if(isset($_POST['place'])){
$place = $_POST['place'];
$date = date("Y-m-d", strtotime($_POST['date']));
$classtype = $_POST['classtype'];
$getdate = mysql_query("SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' limit 0,1")or die(mysql_error());
$mydt = mysql_fetch_array($getdate);
$mdt = date("d-m-Y", strtotime($mydt[classdate]));
echo $mdt;
}
"SELECT * FROM `class` WHERE `city`='$place' AND `clastype`='$classtype' AND `classdate`<'$date' order by `classdate` desc limit 0,1"
Please use order by clause.
Use ORDER BY clause
Try this:
SELECT *
FROM `class`
WHERE `city`='$place' AND `clastype`='$classtype' AND
`classdate`<'$date'
ORDER BY id DESC
LIMIT 0,1

SELECT SUM with date variable, for prestashop

i'd like to have a result on PHP/MYSQL
I have a table ps_orders with price on total_paid
I need to ask total for all price in current date, i have dificult ti insert correct date. I'm stopping here, and do not works... thnaks
....
$date = date("Y-m-d");
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '%$date%'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo mysql_result($result, 0);
Try
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '$date'";
or
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date LIKE '%$date%'";
depending on how your delivery_date field is setup as well as what you are using in your $date variable (time stamp vs just m-d-y), your query up to the where clause looks okay, but you could also try:
SELECT SUM(total_paid)
FROM ps_orders
WHERE delivery_date = $date;
if you are using a datetime field for delevery_date, you'll have to go more in depth and use a range:
SELECT SUM(total_paid)
FROM ps_orders
where (delivery_date > $date
and deliver_date < $date +interval 1 day)
This link should also help you out quite a bit when working with date: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
You can use one of the following:
DATE function in mysql converts 2013-11-20 10:54:12 to 2013-11-20, i.e. truncated a time in date
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE DATE(delivery_date) = '$date'";
or you use only one '%' after $date with using LIKE, so this value will be matched for dates like 2013-11-20 10:12:13 :
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date like '$date%'";
or use string mysql function SUBSTRING
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE SUBSTRING(delivery_date1, 1, 10) = '$date'";
use datetime with time and BETWEEN mysql comparison operator:
$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date1 between '$date 00:00:00' and '$date 23:59:59'

How-to ORDER by DATE with this query

I am trying to change this query to order it by the date correctly.
This line works, but doesn't order all dates correct. It order by Year and not by days and months.
$query = mysql_query
("SELECT `date` FROM `table` WHERE `day` LIKE '%$row->period$row->friday%' AND date != '' ORDER BY `date` DESC LIMIT 1");
while($row2 = mysql_fetch_object($query)){
echo $row->date;
I created this, but the echo stays empty. What did I do wrong?
$query = mysql_query
("SELECT DATE_FORMAT(date, '%d-%m-%Y') as `date2` FROM `table` WHERE `day` LIKE '%$row->period$row->friday%' AND date != '' ORDER BY `date` ASC LIMIT 1");
while($row = mysql_fetch_object($query)){
echo $row->date2;
The syntax was wrong.
SELECT `date` FROM `table`
WHERE `day` LIKE '%".$row->period."%'
OR day like '%".$row->period."%' AND date != '' ORDER BY `date` DESC LIMIT 1")

Select all where date is present of future only

I know this code works:
$eventdate = new DateTime($event['date']);
$datenow = new DateTime();
if($eventdate->format('Y-m-d') < $datenow->format('Y-m-d')) ....
I need to do something similar in an SQL query.
Something like:
SELECT * FROM MyTable WHERE `date` = `$eventdate` or Future Date
How can I do this?
$sql = "
SELECT *
FROM MyTable
WHERE
`date` = '{$eventdate->format('Y-m-d')}' OR
`date` >= CURDATE()
";
or
$sql = "
SELECT *
FROM MyTable
WHERE
`date` = '{$eventdate->format('Y-m-d')}' OR
`date` >= '{$datenow->format('Y-m-d')}'
";
SELECT * FROM MyTable WHERE `date` >= `$eventdate`
Something like:
SELECT
*
FROM
`table`
WHERE
`date_field` >= NOW();
This should work as in this example: http://sqlfiddle.com/#!2/225a3/2

Categories