samanj Posted July 10, 2020 Share Posted July 10, 2020 (edited) Hi, I have made a database linked with php that I use a html form for. The HTML form requests a particular date to retrieve, and the php form it is linked to then retrieves data from the MySQL database from that date. My question is: I want there to be a restriction so that the retrieval can only be for dates before the present date (and certainly not any date in the future). e.g. if it is 6th July 2020, then the database can only retrieve from the 5th July 2020 and before. (7th July 2020 onwards also is not allowed.) Is there a way I can do it? Anyone who can guide me to a particular code function or something of that sort - I would be deeply grateful as my searches have not come up fruitful. Edited July 10, 2020 by samanj clarity Quote Link to comment https://forums.phpfreaks.com/topic/311063-a-php-function-retrieval-ban-for-present-and-future/ Share on other sites More sharing options...
gw1500se Posted July 10, 2020 Share Posted July 10, 2020 if( strtotime($database_date) <= strtotime('now') ) { ... Make it just < if you want to exclude today as well. Quote Link to comment https://forums.phpfreaks.com/topic/311063-a-php-function-retrieval-ban-for-present-and-future/#findComment-1579480 Share on other sites More sharing options...
Phi11W Posted July 10, 2020 Share Posted July 10, 2020 At it's most simplest, your solution is called an "If statement". The HTML page loaded into the User's browser submits data to your PHP for processing. As as essential part of that processing, you should validate the supplied data values against your chosen criteria. In this case, they need to enter a valid [Character Representation of a] Date that must be earlier than the current date. If the supplied data does not match your criteria, then you send error back message(s) that the HTML (or Javascript) has to show to the User. Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/311063-a-php-function-retrieval-ban-for-present-and-future/#findComment-1579481 Share on other sites More sharing options...
Barand Posted July 13, 2020 Share Posted July 13, 2020 (edited) Restrict your db query with a where clause so you only retrieve the records you want SELECT ... WHERE datecol BETWEEN input_date AND CURDATE() - INTERVAL 1 DAY Edited July 13, 2020 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/311063-a-php-function-retrieval-ban-for-present-and-future/#findComment-1579582 Share on other sites More sharing options...
StevenOliver Posted July 14, 2020 Share Posted July 14, 2020 After years of fighting with my server's time zone (shared hosting), I now have these lines of code on my PHP date page (thanks to Barand): date_default_timezone_set('America/Los_Angeles'); $date = new DateTime(); After you implement the suggestions posted above, these two lines of code on your PHP page(s) will save you the aggravation of wondering why your PHP / mySQL seems to always be off by an hour or two :-) Quote Link to comment https://forums.phpfreaks.com/topic/311063-a-php-function-retrieval-ban-for-present-and-future/#findComment-1579601 Share on other sites More sharing options...
samanj Posted July 14, 2020 Author Share Posted July 14, 2020 Thanks all, I will try it out and will reply once successful. Quote Link to comment https://forums.phpfreaks.com/topic/311063-a-php-function-retrieval-ban-for-present-and-future/#findComment-1579632 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.