Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/
Share on other sites

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';	
}
?>

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170928
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170937
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1170983
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171359
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171374
Share on other sites

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 = ''}?>

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171395
Share on other sites

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';
}
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/226939-if-statement/#findComment-1171400
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.