how to display all the possible data greater than the current date - php

$Date = date("m/d/Y");
$result = mysql_query("SELECT * FROM service WHERE SType = 'Retreat' and Start_date > '$Date' ");
Start_date format is m/d/y also.
whats wrong with my code? all i want to do is to display all the possible data greater than the current date. but it always show all the data from the database.

Use date("Y-m-d") rather than date("m/d/Y").

It depends on how you store your date. If its stored as a unixtime, then you simply do an equal or greater check and you are on your way.
PHP has some great functions to turn date into unixtime. Like strtotime

It's best if you use the YYYY-MM-DD format for dates in MySQL.

Have you looked at this: http://www.bigroom.co.uk/blog/dates-in-php-and-mysql ?
The article suggests you consider doing something like
$result = mysql_query(
"SELECT * FROM service WHERE SType = 'Retreat' and Start_date > FROM_UNIXTIME($Date)"
);

Related

How to check if date between two date in php and sql?

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.

get all today's event where the date is stored as int

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.

How to compare date when its save as type text in database

I am using WPDB and here is my SQL
$date = date('d-m-Y');
$reservations = $wpdb->get_results( "SELECT * FROM reservation_db WHERE `date` > '$date'");
I need to select the date in my database when the date in database greater than today.
My date format is dd-mm-yyyy, but I think because it's save in text, it only compares days(dd) which is wrong, any solution to solve this?
MySQL offers a STR_TO_DATE function to convert a date string to date:
SELECT * FROM reservation_db WHERE STR_TO_DATE(`date`) > '$date'
But as ankit suthar mentioned in above comment, it is not recommended to store dates as text.

How to query a table based on timestamp with date in dd-mm-yyyy format

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'";

MySQL 'Between' dates not selecting the correct results using PHP (and Codeigniter)

Here is my code (using CodeIgniter):
$now = date('Y-m-d');
$then = strtotime($now . '-1 week');
$then = date('Y-m-d', $then);
$q3 = $this->db->query("SELECT *
FROM posts
WHERE publish_date BETWEEN '$then' AND '$now'");
$data['posts_today'] = $q3->num_rows();
I clearly have posted at least twenty posts this week, but it only displays '1'. Any ideas why?
Thanks!
Does this work for you:
$q3 = $this->db->query("SELECT *
FROM posts
WHERE publish_date BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK)
AND NOW()");
Potential issues I see:
is POSTS.publish_date a DATETIME data type?
if PHP and MySQL are on different hosts, NOW from codeigniter/PHP could be a different time than what's being stored/used in MySQL.
Verify that MySQL expects dates in that format. You might be better off using MySQL's date functions to compose them.
First, offload everything you can to MySQL. The less date math you have to do the better.
Second, Look at what your actual created code looks like. Change this
$q3 = $this->db->query("SELECT *
FROM posts
WHERE publish_date BETWEEN '$then' AND '$now'");
to this
$sql = "SELECT *
FROM posts
WHERE publish_date BETWEEN '$then' AND '$now'");
print "EXECUTING: $sql\n";
$q3 = $this->db->query( $sql );
You're assuming that the SQL you are generating is valid, and it might not be. This is very common when you're using one language to generate code in another. Always print to verify your assumptions.

Categories