uisneach Posted October 29, 2008 Share Posted October 29, 2008 Hello I need to put a date and time check on my php script. I absolutely need to control future dates. I explain: I have a form with a drop down menu, and what i need is that people choose past date and time. e.g. 1st of October is good but 1st of December, eg. , not So, Here it is (values retrieved) $day = $_POST['day']; $month = $_POST['month']; $year = $_POST['year']; $time = $_POST['time']; //date and time now $time_1 = strtotime ("now"); $today_date = date("d/m/Y", $time_1); $today_time= date("h:i:s", $time_1); SO THE QUESTION is how can i control and stop the entries of future time? if the values of day month, time, etc are > than today date and time, I need to exit. I hope it's clear and thx in advance to eb cheers paol:) Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/ Share on other sites More sharing options...
n3ightjay Posted October 29, 2008 Share Posted October 29, 2008 Im not really sure i understand your question, but i think using mktime() in conjunction with your date function might lead you in the right direction: http://www.php.net/mktime Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677881 Share on other sites More sharing options...
thebadbad Posted October 29, 2008 Share Posted October 29, 2008 If you convert the date and time to a timestamp, you can easily compare it to the current timestamp: <?php $day = $_POST['day']; $month = $_POST['month']; $year = $_POST['year']; $time = $_POST['time']; //may not work depending on what the above variables contain $then = strtotime("$day $month $year $time"); if ($then > time()) { //not okay - the specified date is in the future } else { //okay } ?> If it doesn't work as expected, show me an example of the $_POST variables. Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677893 Share on other sites More sharing options...
uisneach Posted October 29, 2008 Author Share Posted October 29, 2008 Thx I mean, I need to check if the values are referred to a train time to report if it was late or not on a particulare day and time Thats' why I need past time. Cause It's something that must be happened. Nobody can report a train that is late on 1st of december!!!! (e.g) Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677901 Share on other sites More sharing options...
uisneach Posted October 29, 2008 Author Share Posted October 29, 2008 an example could be 05 10 2008 I must compare this value retrieved by the form, with current date. (at the moment is enough also only the date) thx:) Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677916 Share on other sites More sharing options...
thebadbad Posted October 29, 2008 Share Posted October 29, 2008 To only compare dates: <?php $day = $_POST['day']; $month = $_POST['month']; $year = $_POST['year']; $then = strtotime("$year-$month-$day"); $today = strtotime(date('Y-m-d')); if ($then > $today) { //not okay - the specified date is in the future } else { //okay } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677960 Share on other sites More sharing options...
uisneach Posted October 29, 2008 Author Share Posted October 29, 2008 Hello Thebadbad first, thx again 4 ur help i am going to try your script. sorry,i am really (a little more than a) beginner, so I am...u see.. second, it's thesame using mk time to get timestamp and compare the result with unix time? $today = mktime (0,0,0,date("m"),date("d"), date("Y")); Is correct ? with second, i could compare the result from mktime ? bye Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677969 Share on other sites More sharing options...
thebadbad Posted October 29, 2008 Share Posted October 29, 2008 Sure, you can use mktime() if you want. But it's inefficient to use if for the current time; just use time(), or mktime(0,0,0) if you want the stamp for today's date at 00:00:00. Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677970 Share on other sites More sharing options...
uisneach Posted October 29, 2008 Author Share Posted October 29, 2008 thanks really I load oin the server your script withe control , all works So if i would check also the time ? Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677971 Share on other sites More sharing options...
uisneach Posted October 29, 2008 Author Share Posted October 29, 2008 In a few words, i would like to control if the entries date and time , are in the future. and exit(); if it happens. (not only date) is it possibile? thx paol Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-677977 Share on other sites More sharing options...
thebadbad Posted October 30, 2008 Share Posted October 30, 2008 Then use a variation of my first script <?php $day = $_POST['day']; $month = $_POST['month']; $year = $_POST['year']; $time = $_POST['time']; $then = strtotime("$year-$month-$day $time"); if ($then > time()) { //specified date is in the future exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-678226 Share on other sites More sharing options...
uisneach Posted November 7, 2008 Author Share Posted November 7, 2008 Hello. It's okay, thanks Now it works;) cheers Quote Link to comment https://forums.phpfreaks.com/topic/130642-solved-control-date-and-time/#findComment-684830 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.