I sent parameter date to check if that date between two date from database.
example date string parameter that i sent to API is "09/01/2020" which format is mm/dd/YYYY
this format also same for date string in database table column.
Now in database, I put date_start as "08/21/2020" and date_end as "09/05/2020"
My code is :
$date = $data["date"];
to get date string parameter that I sent to API.
Example table name is promo,
my code is :
$sql = "SELECT * FROM promo WHERE STR_TO_DATE('" . $date . "', '%M%M/%d%d/%Y%Y%Y%Y')
between date_start and date_end";
But I did not get it.
How to correct that to get it?
Edited my latest code is like this but still did not get it:
$date = $data["date"];
$date = DateTime::createFromFormat('m/d/Y', $date);
$date = $date->format('m/d/Y');
$sql = "SELECT * FROM promo WHERE $date BETWEEN STR_TO_DATE(date_start, '%m/%d/%Y') AND
STR_TO_DATE(date_end, '%m/%d/%Y')";
You said:
Now in database, I put date_start as "08/21/2020" and date_end as "09/05/2020"
This is not best practice, and you should always store your dates as a proper date or datetime type in MySQL. Had you done this, you would only need a range comparison in your query:
SELECT *
FROM promo
WHERE ? BETWEEN date_start and date_end;
If you must stick with your current design, then you would actually have to use STR_TO_DATE on the start and end dates:
SELECT *
FROM promo
WHERE ? BETWEEN STR_TO_DATE(date_start, '%m/%d/%Y') AND
STR_TO_DATE(date_end, '%m/%d/%Y');
To the ? placeholder above, you should bind either a valid PHP date, or a valid MySQL date literal string. But again, I strongly recommend fixing the design problem.
Related
i need help on this code..Im a newbie on this thing
i would like to query a table to get all today's event where the date&time is stored as int (eg: 1510876800)
i tried it like this
$time = time();
$changedate=date("Y-m-d", strval($time));
$today=strtotime($changedate);
$sql = "SELECT * FROM " . $xoopsDB->prefix('extcal_event') . " WHERE event_start='$today' ORDER BY event_organisateur ASC";
its not working because of the date in event_start have different time
How do i solve this ?
You are comparing unix timestamps so that will only match on the exact same second of that specific day.
If you have a unix timestamp stored, first you need to convert it to a mysql datetime value and then you can get the date part of it:
... WHERE DATE(FROM_UNIXTIME(event_start)) = :today ...
Here the value you bind to :today should have the Y-m-d format.
Note that I have used a placeholder for your variable as you should use prepared statements instead of injecting values directly into your query.
I have a table containing a field "created_at" whose Data Type is timestamp. I donot want to change it to DATE.
Can i query this table to fetch all rows of a day in format dd-mm-yyyy.
Note:
One approach I tried is:
a) Take Date in yyyy-mm-dd concatenate with 00:00:00
b) Take next date in yyyy-mm-dd concatenate with 00:00:00
and use below query to get all records of that day:
SELECT *
FROM news
WHERE created>='2016-12-13 00:00:00'AND
created < '2016-12-14 00:00:00'
Is this a good solution to my problem. Any better approach for this problem.
You can use the MySQL cast() function.
SELECT
*
FROM news
WHERE CAST(created_at AS DATE) = '2016-12-13'
This will discard the time component of your timestamp and do the comparison on only the date.
Reference: http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html
If you don't want to change column datatype then you can go with this code
$startDate = strtotime( '2016-12-13' ); // it will convert date to timestamp format.
$endDate = strtotime( '2016-12-15');
$query = "SELECT * FROM news WHERE created >= '$startDate' AND created < '$endDate'";
I have been trying for a while, read countless stackoverflow answers and still cant crack it!
I have a table in my db with a field called dob. This field is currently just a TEXT field (but i have since tried changing it to a DATE field and still cant get it to work).
The DOB field's data is in this format (UK dates) - 22/05/2016.
Im trying to find out the number of users who's birthdays are between two dates.
For example, anyone who was born in the last two years:
$twoyearsago=date('d/m/Y', strtotime("-2 years"));
$today = date("d/m/Y");
$sql = mysql_query("SELECT * FROM users WHERE dob >= '" . $twoyearsago . "' AND date <= '" . $today . "' ORDER by id DESC");
I also tried:
$sql = mysql_query("SELECT * FROM users WHERE dob BETWEEN '" . date('d-m-Y', strtotime($twoyearsago)) . "' AND '" . date('d-m-Y', strtotime($today)) . "'";
Hopefully you can see where me logic is and hoping you will see where im going wrong - any help would be appreciated.
Jack
With STR_TO_DATE can you convert your date
NOTE: i have changed the Column type from TIMESTAMP to DATE, because in a TIMESTAMP you can store date before 1970-01-01.
SELECT STR_TO_DATE('22/05/2016','%d/%m/%Y');
sample
MariaDB [bb]> SELECT STR_TO_DATE('22/05/2016','%d/%m/%Y');
+--------------------------------------+
| STR_TO_DATE('22/05/2016','%d/%m/%Y') |
+--------------------------------------+
| 2016-05-22 |
+--------------------------------------+
1 row in set (0.00 sec)
MariaDB [bb]>
so you can change you Table
ALTER TABLE `users`
ADD COLUMN new_dob DATE;
UPDATE `users` SET new_dob = str_to_date(dob,'%d/%m/%Y');
** Verify the dates
ALTER TABLE `users`
DROP COLUMN dob;
ALTER TABLE `users`
CHANGE COLUMN `new_dob` `dob` DATE;
** CREATE an INDEX for perfomance **
ALTER TABLE `users`
ADD KEY (`dob`);
SELECT
SELECT * from `users` where dob between '2014-01-01' AND `2015-08-01';
The problem with many local date formats is that their lexical and chronological order are different (eg, 16-11-2016 comes after 11-12-2016 lexically, but before chronologically). That's why storing dates in string fields in some regional format is in most cases a bad idea: you will get sorting issues sooner or later.
Next, when specifying dates literally for MySQL, you have to respect certain formats, as explained in the documentation
Putting that into practice, the range variables should look something like this:
$today = date("Y-m-d");
$twoyearsago=date("Y-m-d", strtotime("-2 years"));
Then we use a built-in function str_to_date to convert the string column into a date that can be compared correctly:
SELECT * FROM users WHERE
STR_TO_DATE(dob, '%d/%m/%Y') between '$twoyearsago' and '$today'
This will work, but in the long run you're much better off converting that dob column into a real date format (as #BerndBuffen shows) as it's clearer, easier to internationalize and a lot better performing.
Sidenote: you are still using the long-deprecated mysql_ extension. You should really switch to either mysqli_ or PDO.
You need to build your query by using actual date values, not string. So you need format YYYY-MM-DD in query - both side of the comparison.
Try following.
$twoyearsago=date('Y-m-d', strtotime("-2 years"));
$today = date("Y-m-d");
$sql = mysql_query("SELECT * FROM users WHERE STR_TO_DATE(dob, '%d/%m/%Y') >= '" . $twoyearsago . "' AND STR_TO_DATE(dob, '%d/%m/%Y') <= '" . $today . "' ORDER by id DESC");
STR_TO_DATE(dob, '%d/%m/%Y') makes sure your d/m/Y saved dob string value to be converted to date in the query that MySQL can understand and compare with the given YYYY-MM-DD values.
Actually the proper way is creating a date field and transferring dob string values as date to this new field by using the same function unless you will always get the date values as string into the dob field.
Another method is to use DateTime and format the date before doing your query.
$begin = '10/02/2014';
$emd = '10/02/2015';
$beginDate = DateTime::createFromFormat('d/m/Y', $begin);
$emdDate = DateTime::createFromFormat('d/m/Y', $emd);
$stmt = "
SELECT
...
FROM users
WHERE birthday >= '".$beginDate->format('Y-m-d')."'
AND birthday <= '".$endDate->format('Y-m-d')."'
";
I am attempting to run a query based on a date to get everything changed today, however the field that has the date is a datetime field, which I can't change. The value in my date_modified field looks like 2013-10-25 07:05:43. I know the query works without the date. If I just use Navicat and run the query's where clause as:
WHERE inventory.date_modified like '2013-10-25%'
It runs fine. I found a few ideas in other SO posts and when I try to put it into my php code as:
$today = date("Y-m-d"); $today = '%' . $today . '%';
and then my where clause is:
WHERE inventory.date_modified like $today
I get the fatal error of trying to parse a non object. I know there is a value in $today, but I am unclear how to do this with my php code?
Best solution was the N.B.'s in the question comment. Because, if you have index on field date_modified, it will be used.
So, if you have date formated as Y-m-d, like you have in $today, you can do:
$sql = "
...
WHERE inventory.date_modified BETWEEN '$today 00:00:00' AND '$today 23:59:59'
...
";
Another example would be, to use DATE() function, to get date part from datetime:
$sql = "
...
WHERE DATE(inventory.date_modified) = '$today'
...
";
but, this example is not good if you have index on field date_modified, because it will be ignored, and whole table will be scanned.
You Forget to set the quotes on database value:
WHERE inventory.date_modified like '$today'
Try this code:-
$today = '2013-10-25';
SELECT * FROM inventory WHERE DATE_FORMAT(inventory.date_modified, "%Y-%m-%d") = $today;
I need to match the result format of 2 code:
I need to get the output/format of this:
$event_day = $year.'-'.$month.'-'.$list_day; // $event_day
match this:
DATE_FORMAT(date,'%Y-%m-%d')
Full code:
$query = "
SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date
FROM table
WHERE user_id = '$session_user_id'
AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "'
AND active = 1";
My problem is that $event_day is only displaying events for: October, November and December.
I had a similar problem with this code below:
$query = "
SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date
FROM table
WHERE user_id = '$session_user_id'
AND date LIKE '$year-$month%'
AND active = 1";
and it was fixed with this code:
$query = "
SELECT title, DATE_FORMAT(date,'%Y-%m-%d') AS date
FROM table
WHERE user_id = '$session_user_id'
AND date BETWEEN '$year-$month-1' AND '" . date("Y-m-t", strtotime("$year-$month-1")) . "'
AND active = 1";
Anyone know how I could sort this out?
If you are simply trying to match dates in a SQL query, which is what you look like you're after, you can just pass the date as a string with a # on each end. This will tell SQL to parse the string as a date and the format you give it in is not really important. The exception to this is it can get confused between US/EU date formats like dd/mm/yyyy vs mm/dd/yyyy.
$query = "SELECT title, DATE_FORMAT(myDate,'%Y-%m-%d') AS formattedDate
FROM table
WHERE user_id = '$session_user_id'
AND (myDate BETWEEN #$year-$month-1# AND #$year-$month-1#)
AND active = 1";
This format should work but your query is select records that fall between the same date so you'll only get records that are actually on that date. You might as well just use WHERE date = #$year-$month-1#
edit: this assumes your date field datatype is correctly set up in the database as a date/time.
edit2: "date" is often a reserved word in databases and shouldn't really be used for field names and variables. See edited code above.
In addition I would suggest using php to format the date using the date() function rather than format it in the query. This allows more flexibility to actually use the date in your script.