Basically i'm doing a simple wedding planner and i am trying to check whether the date that the user has inserted as available or whether the venue is booked. Basically, it's falling over with my SQL query. Initially i am setting the time:
$time = mktime(0, 0, 0, $month, $day, $year);
Then i have an sql query where i am checking it against an id variable that is previously set and the date that is passed through.
$sql2 = "SELECT * FROM venue_booking WHERE date_booked = ".$time." AND venue_id =".$id;
In this case the date in the database is 2015-01-01 and the date i'm passing through is 2015-01-01, I am checking this in an if statement if the amount of rows returned from the database is greater than 0 then echo booked, else echo available.
Even if the output is meant to say booked it still says available. Is there an issue with the way i am checking the time against mysql's date.
date_booked - This is a MySQL date (2014-01-01)
Anybody have any ideas?
Your current query is missing quotes around your date string so it wouldn't work as it is.
But to answer your question, just pass a valid date string in YYYY-MM-DD format and your query would work:
$date = $_POST['date'];
// Put date validation code here. I.e. make sure it is in YYYY-MM-DD
// format, etc. Might as well escape it, too since you aren't using
// prepared statements.
$sql2 = "SELECT * FROM venue_booking WHERE date_booked = '".$date."' AND venue_id =".$id;
I should also mention that you should probably switch to using prepared statements as it will make using user-provided data in queries safer.
Here is a possibly useful example of date validation. If you need to convert the date from one format to another, this will show you how.
mktime() returns a PHP timestamp, which is a unix timestamp, which is "seconds since the epoch", aka Jan 1/1970. You cannot directly compare that against a mysql date field. You'd literally be doing
WHERE '2014-01-01' = 12345678
Try
$ts = date('Y-m-d h:i:s', mktime(0,0,0,$month, $day, $year));
$sql = "SELECT ... date_booked = '$ts'";
Related
In database I am storing date value unix timestamp value for e.g.'1434952110' using time() now I am trying to compare todays value with this value as below
$jobpostdate = date("Y-m-d");//today's date passing in database to compare
query
$sql = "SELECT jsp_title, jsp_subtitle, jsp_desc, jsp_location, jsp_date ";
$sql .= "FROM js_projects WHERE DATE(jsp_date) = '$jobpostdate' ";
I tried above query , but even if the value is present I am getting no rows found, where i am going wrong how can I compare date values ? I know how to compare in php but i don't know exactly how to check in query ,Pls any one can help
If I understand correctly you have a unix timestamp in a varchar field and you can't change this.
If you compare the unix timestamp directly you will only get results that match the exact second of the timestamp.
You can use FROM_UNIXTIME() to convert the timestamp in a date value you can actually use:
WHERE DATE(FROM_UNIXTIME(jsp_date))=CURDATE()
This compares the date portion of the timestamp with the current date, giving you all database entries of the given day.
Note: This could create performance problems, since a lot of conversions occur on every request. You should really convert the unix timestamp in the database into a DateTime or Date field.
You could use PHP DateTime class.
$today = new DateTime('now'); // Can be what ever you want, not only now
// see below for reference.
$jobpostdate = $today->getTimestamp();
and query:
$sql = "SELECT jsp_title, jsp_subtitle, jsp_desc, jsp_location, jsp_date ";
$sql .= "FROM js_projects WHERE jsp_date = '$jobpostdate' ";
!PS I can't test it, but in a nutshell it should work.
Fore more reference about this awesome class go here:
http://php.net/manual/en/class.datetime.php
I have a Database with column name DT, that has values:
1. 16-05-2015
2. 16-05-2015
3. 30-06-2015
4. 30-06-2015
Sql query in php that I am using is:
$sql = "Select * from logs WHERE (DT<= '$to') AND (DT>= '$from') ";
I have also used query:
$sql = "Select * from logs WHERE (DT BETWEEN '$to' AND '$from') ";
Both the queries are not functioning in the same way as required.
If $to=31-05-2015 and $ from = any date before 31st, the query displays the 30-06-2015 date as well, but it is not in the range.
When $to=30-05-2015, correct results are displayed but choosing any date after 30-05-2015 does not display the correct range, what could be the reason of it?
You have to change your date format to yyyy-mm-dd while saving it to the database and change it to the dd-mm-yyyy while displaying, and also change the datatype of the column to the datetime to make you query work
$newdate = date("d-m-Y", strtotime($yourdate));
apparently, DT is not configured as date in your DB.
And thus the values are ordered lexigoraphically, as strings.
(In lexigoraphical order: 30-... is before 31-..., no matter what ... might be)
You can either change your data to be saved as yyyy-mm-dd strings.
Or configure the column correctly as date
I am trying to do a query into a database to return a report with php. I am using duplicate pages as they both are pulling from the same database/tables.
The specific error I receive is:
Msg 241, Level 16, State 1, Line 1
Syntax error converting datetime from character string.
This query works and returns the desired data:
REMOVED SELECT STATEMENT PER COMPANY POLICY
This query however, does not work:
REMOVED SELECT STATEMENT PER COMPANY POLICY
I changed the DateUpdated to DateAdded in the cast portion. The Date/Time are obtained from a Java date picker script. Everything on the two pages are exact, except for the sql query. Does anyone have any idea what could be the problem? I am fairly new to PHP and so I am learning as I go.
I have tried searching but I do not understand what I should do.
I have these declared from the picker....
$startunix = date("Y-m-d H:i:s", strtotime($startdate));
$startunixmn = date("Y-m-d 23:59:59", strtotime($startdate));
$endunix = date("Y-m-d H:i:s", strtotime($enddate));
$endunixmn = date("Y-m-d 23:59:59", strtotime($enddate));
and within the sql query, in place of the dates/times that are showing in the statement, they have $startunix and $endunixm for the two dates/times. Are you not allowed to change what is in the cast? I thought you would be able to, because both columns are dates in the format 010109 (mmddyy). SO I figured if you change the DateUpdated to DateAdded (titles of the two columns) then the query should work, just based off of the date range selected for DateAdded instead of DateUpdated.
Any help or insight would be hugely appreciated. If you need more info, please ask and I will do my best to provide what I can.
In your case you will avoid the mentioned error by checking the if the DateAdded and DateUpdated are not null. Add the DateAdded IS NOT NULL in where clause.
It would be the best to insert once prepared value in database instead of parsing it every time you query the db. You said you are using:
$startunix = date("Y-m-d H:i:s", strtotime($startdate));
$startunixmn = date("Y-m-d 23:59:59", strtotime($startdate));
$endunix = date("Y-m-d H:i:s", strtotime($enddate));
$endunixmn = date("Y-m-d 23:59:59", strtotime($enddate));
but where does the string in mmddYY format then comes from? If you populate the DateAdded and DateUpdated fields with proper date (check it here) you will be able to avoid making a new value from DateAdded field every time. Also if you don't need time, you can use Y-m-d format instead of Y-m-d H:i:s and will be able to shorten your query to something like this:
SELECT CardNo as CardNo, LastName as CLname, FirstName as CFname, Email as CEmail ,DateUpdated as CUpdated, DateAdded as CEntered FROM cards
WHERE recemail = 'YES' AND email IS NOT NULL AND email <> '' AND (DateAdded BETWEEN '2013-07-29' AND '2013-08-02') ORDER BY DateAdded desc, CardNo
I have a while statement to echo some data to the user from specific dates.
I have stored the dates in my data base in this format: time() I want to write my sql statement in this format to check only the date date("m/d/y", some time). like so:
$page_visits_count = $mysqli->query("SELECT COUNT(`id`) FROM `visits` WHERE date('m/d/y', `time`) = '$date' ");
This part date('m/d/y', ``time``) is obviously wrong, but how would I be able to fix this problem ?
Note: I tried adding one day (3600 * 24) to my time. But that sometimes put me the middle of the next day.
If I understand correctly you're wanting to convert a UNIX timestamp (stored in DB column time) into a date format, then compare that date to the $date variable.
If that's true, FROM_UNIXTIME is what you're probalby after:
"SELECT COUNT(`id`) FROM `visits` WHERE FROM_UNIXTIME(`time`, '%m/%d/%Y') = '$date'");
You'll probably want to tweak the %m/%d/%Y part to match the format of your $date variable, see the FROM_UNIXTIME docs I linked to for a list on what formats are available.
im trying to retrieve a set of id's that are due to expire within the next 7 days, and for the life of me cant get my query to work
$target_date = date('Y-m-d', strtotime('+7 days'));
$sql = "SELECT subscriptions.`id` FROM subscriptions WHERE date($target_date) <= expires AND cust_id =$_SESSION[uid]";
any help would be greatly appreciated
This is fine:
$target_date = date('Y-m-d', strtotime('+7 days'));
As it structures the date in the same way as MySQL [YYYY-MM-DD]
I've switched your query around and applied the DATE() function to the 'expires' field and not your $target_date as it doesn't need to. Your expires field might be in [YYYY-MM-DD HH:MM:SS] format, which might be why it's not working:
$sql = "
SELECT subscriptions.id
FROM subscriptions
WHERE DATE(expires) >= '$target_date'
AND cust_id = '$_SESSION[uid]'";
Echo out the $sql query to find out if its executing it properly and Use the or die(mysql_error()); to tell you where it's failing.
Use SQL escaping too (Either PDO or mysqli) as has been outlined in another answer.
Hope this helps.
If you create a date in a roughly MySQL compatible format, it should work without having to use the DATE function, just compare it as if it were a string.
Your code is in dire need of some proper SQL escaping that would fix this problem and likely a worryingly large number of others in your application.
The query running in PDO or mysqli would look roughly like:
$target_date = date('Y-m-d', strtotime('+7 days'));
$sql = "SELECT subscriptions.`id` FROM subscriptions WHERE ?<=expires AND cust_id=?";
You can then call the appropriate bind_param method to link in the right values to those placeholders.
You can do it with MySQL:
SELECT subscriptions.`id` FROM subscriptions WHERE date_add(curdate(),interval 7 day) <= expires AND cust_id =$_SESSION[uid]