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'
Related
I want to fetch all records between two dates from database in php. Date format is dd/mm/yyyy hh:mm:ss. example 06/Dec/2016 05:56:15
I'm using following code
$Sdate=date_create($_GET['sdate']);
$start=date_format($Sdate,"d/M/Y H:i:s");
$Edate=date_create($_GET['edate']);
$end=date_format($Edate,"d/M/Y H:i:s");
$sql = "SELECT * FROM `payments` WHERE `customerid` = '".$_SESSION['id']."' AND dateandtime BETWEEN ('".$start."', '".$end."') ORDER BY id DESC";
But this is not working
Thank You in advance for helping me
Try this:
$Sdate = date('Y-m-d H:i:s', strtotime($_GET['sdate']);
$Edate = date('Y-m-d H:i:s', strtotime($_GET['edate']);
$sql = "SELECT * FROM `payments`
WHERE `customerid` = '".$_SESSION['id']."'
AND dateandtime BETWEEN '$Sdate' AND '$Edate'
ORDER BY id DESC";
The correct syntax is:
dateField BETWEEN dateFieldLow AND dateFieldHigh
$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
");
I am trying to select the date in the field end_date that is less or equal to current date but did not work and my field end_date as the same date format(08-09-2014) data as current date below is my code thanks
$currentdate = date("d-m-Y");
$query1 = "SELECT * FROM location WHERE end_date <= '$currentdate'";
$result1 = mysql_query ($query1) or die('query error');
while( $line1 = mysql_fetch_assoc($result1)){
echo $line1['end_date'];
}
try this
SELECT * FROM location WHERE end_date <= DATE_FORMAT(CURDATE(), '%Y-%m-%d')
try with -
SELECT * FROM location WHERE end_date <= DATE_FORMAT(CURDATE(), '%d-%m-%Y')
$currentdate = date("Y-m-d");
$query1 = "SELECT * FROM location WHERE `end_date` <= '$currentdate'";
$result1 = mysql_query ($query1) or die('query error');
while( $line1 = mysql_fetch_assoc($result1)){
echo $line1['end_date'];
}
try this... must have in database date format;;
If your currentdate is independent of the system date (e.g. if you are operating over different timezones), and if, for some reason, your end_date is not a date type, then try this:
SELECT * FROM location
WHERE str_to_date(end_date, '%d-%m-%Y') <= str_to_date('$currentdate', '%d-%m-%Y')
where the format can be changed to match your inputs.
If you want to compare dates, then make sure you are comparing dates and not strings.
hello im having some problem with this sum select does anyone know whats wrong with this, it seems that im getting no result from it
$result = mysql_query("SELECT SUM(total)
FROM table1 where date BETWEEN '".$date1." 00:00:00' AND '".$date2." 23:59:59' and username = ".$user."");
while($row=mysql_fetch_array($result))
{ $sum = $row['SUM(total)'];}
Try
$query = "SELECT SUM(total) AS result
FROM table1
WHERE DATE(date) BETWEEN '$date1' AND '$date2'
AND username = '$user'";
$con = \\your db connection string
$result = mysql_query($con,$query);
while($row=mysql_fetch_array($result))
{$sum = $row['result'];}
I want to check if the current year/month/day/week/hour is the same as the dateTime saved in the db.
The current dateTime is: 2013-12-15 12:02:19
The db dateTime is: 2013-12-15 12:00:00
This is my query that checks the year/month/day/week:
$query_topics = mysql_query("SELECT * FROM topics WHERE day(startTime) = day(CURDATE()) and month(startTime) = month(CURDATE()) and day(startTime) = day(CURDATE()) and week(startTime) = week(CURDATE())") or die (mysql_error());
This one works but when I add hour to the query he will give me back null rows.
This is the query with year/month/day/week/hour:
$query_topics = mysql_query("SELECT * FROM topics WHERE day(startTime) = day(CURDATE()) and month(startTime) = month(CURDATE()) and day(startTime) = day(CURDATE()) and week(startTime) = week(CURDATE()) and hour(startTime) = hour(CURDATE())") or die (mysql_error());
Try replacing CURDATE() with NOW():
hour(NOW())