leachus2002 Posted February 7, 2011 Share Posted February 7, 2011 Hi All, I have an SQL query that returns a date (31/01/2011 - for example) and this get's placed into a variable - $req_date. I want to be able to display an image when the required date is within 5 days of the current date. Normally in an if statement I would use: <?php if ($ticket_no == 100) { echo "This is ticket 100" } else { echo "This is not ticket 100" ?> However I am struggling to work out the difference between the 2 dates. Any ideas? Cheers Matt Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/ Share on other sites More sharing options...
JonnoTheDev Posted February 7, 2011 Share Posted February 7, 2011 Have a play with this <?php // the number of days from todays date we will add to our range $numdays = 5; $days = array(date('d/m/Y')); for($x = 1; $x <= $numdays; $x++) { $days[] = date('d/m/Y', strtotime('+'.$x.' days')); } // the date value to test $totest = '31/01/2011'; if(in_array($totest, $days)) { print 'This date is within my accepted range of dates'; } else { print 'This date is NOT within my accepted range of dates'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170928 Share on other sites More sharing options...
doddsey_65 Posted February 7, 2011 Share Posted February 7, 2011 an easier method would be: $req_date = '03/02/2011'; $req_date = strtotime($req_date); $req_date >= strtotime('-5 DAYS') ? $result='Yes' : $result='No'; echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170933 Share on other sites More sharing options...
leachus2002 Posted February 7, 2011 Author Share Posted February 7, 2011 Hi doddsey_65 I like this method, however it is not "echoing" correctly, I believe - let me show my code: while ($unassigned = mssql_fetch_assoc ($unassigned_tickets)){ $pull_date = $unassigned['required_date']; $req_date = $pull_date; $req_date = strtotime($req_date); $req_date >= strtotime('-5 DAYS') ? $result='Yes' : $result='No'; echo '<tr><td>$result</td></tr>'; Cheers Matt Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170937 Share on other sites More sharing options...
beegro Posted February 7, 2011 Share Posted February 7, 2011 Try it this way while ($unassigned = mssql_fetch_assoc ($unassigned_tickets)){ $pull_date = $unassigned['required_date']; $req_date = $pull_date; $req_date = strtotime($req_date); $result = ($req_date >= strtotime("-5 days")) ? 'Yes' : 'No'; echo '<tr><td>$result</td></tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170983 Share on other sites More sharing options...
doddsey_65 Posted February 8, 2011 Share Posted February 8, 2011 theres nothing wrong with the if statement its your echo: echo '<tr><td>$result</td></tr>'; anything inside single quotes doesnt get parsed by php therefore it will just echo $result. try: echo '<tr><td>'.$result.'</td></tr>'; Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171352 Share on other sites More sharing options...
JonnoTheDev Posted February 8, 2011 Share Posted February 8, 2011 Try it this way while ($unassigned = mssql_fetch_assoc ($unassigned_tickets)){ $pull_date = $unassigned['required_date']; $req_date = $pull_date; $req_date = strtotime($req_date); $result = ($req_date >= strtotime("-5 days")) ? 'Yes' : 'No'; echo '<tr><td>$result</td></tr>'; This is incorrect. The original post is stating that the condition is met when the tested date is within 5 days of the current date. What you are doing using the strtotime function is returning the value 'Yes' to $result when the tested date is greater than 5 days in the past. This means that if the tested date is 30 days in the future then it will also return 'Yes', although the requirement states that this should return false. It will be the same with past dates if you reverse the function to use the correct +x days as opposed to -x days. What you are missing is a second condition in the if statement to say if the tested date is greater than or equal to the current day and also less than or equal to 5 days in the future. The reason I used an array containing 5 dates was to keep it simply to date values. Using strtotime without formatting the date you are dealing with timestamps (date + time). In this case you are not interested in the time which could alter the value of the output. There is nothing wrong with the way you have gone about tackling the problem, however in terms of making it simple to debug and adjust (also be able to see the values you are testing for) as opposed to the least number of lines of code I would prefer the code I posted. This is tested and works without issue as opposed the the incorrect code you are struggling with. Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171359 Share on other sites More sharing options...
leachus2002 Posted February 8, 2011 Author Share Posted February 8, 2011 Hi Neil, I have re-coded using your code, and it is working fine - I can see what you mean with the previous code that I used, it was highlighting more than I needed! Is there any way with your code that it can also highlight passed (previous) dates? Thanks Matt Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171374 Share on other sites More sharing options...
JonnoTheDev Posted February 8, 2011 Share Posted February 8, 2011 Is there any way with your code that it can also highlight passed (previous) dates? Of course. What range? Do you require 5 days either side of the current date. i.e 10 future days from 5 days in the past. Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171385 Share on other sites More sharing options...
leachus2002 Posted February 8, 2011 Author Share Posted February 8, 2011 Hi Neil, I would like it to warn at less than 5 days until required_date, and also when the required date has passed. This is how I am using your code: <?php // the number of days from todays date we will add to our range $numdays = 5; $days = array(date('d/m/Y')); for($x = 1; $x <= $numdays; $x++) { $days[] = date('d/m/Y', strtotime('+'.$x.' days')); }// the date value to test $totest = '31/01/2011'; if(in_array($totest, $days)) { $result = '<img src="error.png"/>'; }else { $result = ''}?> Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171395 Share on other sites More sharing options...
JonnoTheDev Posted February 8, 2011 Share Posted February 8, 2011 I would like it to warn at less than 5 days until required_date, and also when the required date has passed. If you want the previous 5 days as oppsed to the next 5 days you simply change the following line // from $days[] = date('d/m/Y', strtotime('+'.$x.' days')); // to $days[] = date('d/m/Y', strtotime('-'.$x.' days')); There is something that you should fundementally change in your code, database, etc. That is your date formatting. PHP / MYSQL work with US formatted dates YYYY-mm-dd. If you have dates in your database you should change the field type to DATE if they are stored in VARCHAR fields. UK formatted dates are not so great for using in MYSQL or PHP functions i.e if you were to do the following: print date('Y-m-d', strtotime('+1 day', strtotime('03/02/2011'))); You would expect the next day to be 04/02/2011. PHP will actually return 03/03/2011. So we need to change your date formatting <?php // the date value to test $totest = '05/02/2011'; // convert to US date format YYYY-mm-dd $date = explode('/', $totest); $date = $date[2].'-'.$date[1].'-'.$date[0]; // the number of days before todays date we will add to our range $numdays = 5; $days = array(date('Y-m-d')); for($x = 1; $x <= $numdays; $x++) { $days[] = date('Y-m-d', strtotime('-'.$x.' days')); } if(in_array($date, $days)) { print 'This date is within my accepted range of '.$numdays.' dates'; } else { if(time() > strtotime($date)) { print 'This date has passed'; } else { print 'This date is NOT within my accepted range of dates'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171400 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.