Select all where date is present of future only - php

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

Related

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

How can i show Data from mysql specific month

$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
");

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 do I select MySQL Data that are from next year

I am working on a file management project, where I have expiry dates for every file. I need to list all the files that are going to expire the next year. What will be the SQL query?
should it be something like:
$date = date ('Y-m-j');
$newdate = strtotime ( '+1 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
$sql = "SELECT * FROM `files` WHERE `expDate` = year ($newdate)" ;
Try:
$sql = "SELECT * FROM `files` WHERE YEAR(`expDate`) = YEAR(NOW()) + 1" ;
If the expDate is in mysql timestamp(yyyy-mm-dd hh:mm:ss) , you can use this
DATE_ADD(`expDate`, INTERVAL 1 YEAR)
Try this
$newdate = date ( 'Y-m-j' , strtotime('+1 year') );
$sql = "SELECT * FROM `files` WHERE `expDate` = year ($newdate)" ;
or
$sql = "SELECT * FROM `files` WHERE `expDate` = YEAR(NOW())+1;
you can use mysql DATE_ADD to add years
$sql = "SELECT * FROM `files` WHERE `expDate` = YEAR(DATE_ADD($date, INTERVAL 1 YEAR))";

PHP For Loop through date range not working

I'm trying to loop through every day in a month and get the number of results for each day in one month.
I've done the following;
<?php
include 'inclues/db.inc.php';
for($i = 2; $i < 30; $i++){
$date2 = $i-1;
$date1 = $i;
$q = mysql_query("SELECT * FROM `table` WHERE `date` < '2012-09-".$date1." 00:00:00' AND `date` > '2012-09-".$date2." 00:00:00'";
//$r = mysql_fetch_assoc($q);
echo mysql_num_rows($q)."<br />";
}
?>
It works if I just try to echo out dates 1 and 2 but not if I use the query.. I end up with a 500 internal server error when using the query.
Any ideas on how to resolve this? Is it generally bad practice to use a query within a loop?
Just execute
SELECT date, COUNT(*) from table group by date
SELECT * FROM `table` WHERE `date` BETWEEN $date1 AND $date2;
It's not bad practice to use a query within a loop.
Check what your query-string is becomming, try running that query manually on your db. It probably produses some error.
$sql = "SELECT * FROM `table` WHERE `date` < '2012-09-".$date1." 00:00:00' AND `date` > '2012-09-".$date2." 00:00:00'";
echo $sql;
$q = mysql_query($sql);

Categories