SQL Select: Multiple values [duplicate] - php

This question already has answers here:
Datetime equal or greater than today in MySQL
(10 answers)
MySQL Where DateTime is greater than today
(3 answers)
Selecting entries by date - >= NOW(), MySQL
(6 answers)
Select record(s) from mysql table where date is greater than or equal to today
(3 answers)
MySQL query select all were date is equal to today on datetime
(2 answers)
Closed 3 years ago.
Would somebody mind helping me with this?
I'm trying to select all bookings from my table with a start date of today or in the future. The issue i'm having is that I already have a limit (pagination) and sort.
$today = date("Y-m-d");
$sql = "
SELECT *
FROM `bookings`
WHERE startdate >= $today
ORDER BY `startdate` ASC
LIMIT $offset, $no_of_records_per_page";
Edit: The code above displays all bookings regardless of date, rather than just present/future bookings.

Look at the generated SQL statement -- the current date in the SQL query should be something like '2020-01-02' (including the single quotes). Without the quotes, you'd get weird behavior like the database doing the subtraction and comparing the date against 2020-01-02 = 2017!.

WHERE startdate >= $today
Your immediate problem is that you are not surrounding the date variable with single quotes, so you end up with something like where start_date >= 2020-01-13. MySQL sees an arithmetic operation (2020 minus 1 minus 13 = 2006) and happily executes it. Now it needs to compare column startdate (which is of date datatype or the like) to an integer: for this, it implicitely casts startdate to unsigned: this generates a number like 20200113, which is much bigger than 2006. This is not what you intend.
This woud not happen if you were using parameterized queries.
But bottom line, why bother computing the current date from PHP when MySQL has a built-in for that? Just do: WHERE startdate >= CURRENT_DATE and you are all set.

Related

SQL - Selecting DATE FORMAT from table and displaying on screen [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Query to convert from datetime to date mysql
(5 answers)
Parse date in MySQL
(2 answers)
Closed 4 years ago.
I have inserte date into my table and it is in the format YYYY-MM-DD, However when displaying this information I would like to display the date the in DD-MM-YYY format. If anyone has an idea how to achieve this please let me know. Please see my sql query below as this query it not working.
// Create query using SQL string
$sql_query = "SELECT title, level, DATE_FORMAT(dateTo, '%W %M %e
%Y')
FROM jobPost ORDER BY jobID DESC";
// Query database using connection
$result = $conn->query($sql_query);
You can use the following:
DATE_FORMAT(dateTo, '%d-%m-%Y')
/* For dateTo value of 2018-11-13, it will output 13-11-2018 */
Details:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%Y Year as a numeric, 4-digit value
Check the complete list of available format specifiers at: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format

how to find the next closest date in with my given date mysql and php [duplicate]

This question already has answers here:
Get closest date from MySQL table
(3 answers)
Closed 7 years ago.
Hi i have some examples dates given below.How to find the next closest date in php or in mysql ?
I have some date like 12-04-2015 so now i need to get the closest date after the given date.So in my case my closest date is 15-04-2015.How can i find using PHP and mysql ?? Any one help me
PHP
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
Mysql
SELECT Birthdate FROM hedging ORDER BY ABS(DATEDIFF(Birthdate , `2015-04-12`)) LIMIT 1
How can i execute the above query in mysql ?
BirthDate
25-03-2015
10-04-2015
10-04-2015
11-04-2015
15-04-2015
30-04-2015
You've not given us much information about what you've tried.
In PHP, store the dates in an array, you can then simply sort it and choose the next value.
In SQL so long as they are stored as dates, then WHERE date > $date ORDER BY date DESC LIMIT 1 will give you the next date.

How to check a particular date is in between two different dates? [duplicate]

This question already has answers here:
mysql: get record count between two date-time
(4 answers)
Closed 8 years ago.
I am working in mysql database, and I have a table called bookings in that two columns named start_date and end_date And I get a date value from my php script
Can anybody tell me how can I check that date is in between those start_date and end_date
I have tried this:
mysql_query("SELECT * FROM `tbl_booking` WHERE '$date' between Start_date AND end_date");
BETWEEN assumes that you have some values and comparing column with them. I believe it should be something like: WHERE Date(start_date) <= '$date' AND Date(end_date) >= '$date'
PS: Not sure about PHP's var interpolation though. Added typecast from one of answers, that seems to be correct.
if you have column type Date then try
mysql_query("SELECT * FROM tbl_booking WHERE date_column between Start_date AND end_date");
if Datetime then
mysql_query("SELECT * FROM tbl_booking WHERE Date(date_column) between Start_date AND end_date");

create time stamp and compare time by mysql field [duplicate]

This question already has answers here:
MySQL convert date string to Unix timestamp
(4 answers)
Closed 8 years ago.
there is a table with these fields
month
year
day
and these files are contain numbers like
year = 2001 and month = 10 and day = 25
and i have time stamp in php
so i need to compare these in mysql
something like:
select * from times where mktime(year,month,day)<1235478554
is there any function in mysql for this?
i need to make time stamp in mysql with the mysql fields...
UPDATE 1 :
i used like this and not worked
SELECT * from work_result where UNIX_TIMESTAMP('year-month-day 00:00:00')<1
UPDATE2:
There's UNIX_TIMESTAMP which will get you the Unix timestamp of a given date string. Which means you need to build a date string inline in your MySQL query. Doing so is considered bad style, you should propably be storing a Unix timestamp within your database.

How do I make now() be yesterday [duplicate]

This question already has answers here:
MySQL selecting yesterday's date
(8 answers)
Closed 9 years ago.
I want to set dealend to be Yesterday instead of now. Is there a way to do it in this command line?
$query = mysqli_query($myConnection, "UPDATE bookdeals SET dealend='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));
... SET dealend = now() - INTERVAL 1 DAY
Note that with the 'now()' you're using, you're not using a function called "now". You're trying to set your table field to be a string whose contents are the letters n, o, w etc.... Quotes turn things into strings and those things lose their specialty once they've been stringed.
now() is a SQL function that is determined by the starting time of the transaction. You can changed the value of it by calling
SET TIMESTAMP = ...
However, for this to work it would need to be in the same transaction as your UPDATE query. What I would recommend instead is doing something like:
UPDATE bookdeals SET dealend=DATE_SUB(NOW(), INTERVAL 1 DAYS) WHERE id='$pid'
This uses the MySQL SUB_DATE() function, which will subtract one day from the current time.
You could use the MySQL DATE_SUB function
... dealend=DATE_SUB(now(),INTERVAL 1 DAY) ...
Read here for more information about MySQL date function

Categories