Menu
Hi all,
I’m having some trouble and just looking for a little guidance. Just need to create a basic page that takes a date from user input and determines if it is at least 3 days before or after the current date. Thanks.
[code=php]$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(0,0,0,date('m'),date('d')+3);
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime)
{
echo 'pass';
}
else
{
echo 'fail';
}[/code]
[code=php]$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(23,59,59,date('m'),date('d')+3);
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime)
{
echo 'pass';
}
else
{
echo 'fail';
}[/code]
Boredom is a terrible thing...?[code=php]$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(23,59,59,date('m'),date('d')+3);
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime)
{
echo 'pass';
}
else
{
echo 'fail';
}[/code]
Boredom is a terrible thing...?[/QUOTE]
That's debateable. Most people would say 1 day's time is tommorrow, not the day after... and one day ago is yesterday, not the day before. At the minute we are at 3 days plus remainder in both directions. Or is three days time to start from now and not midnight?Thank you. I'm quite new to web development. I'll apologize in adavnce for asking this but by passing '2007/03/05' into the $userdate variable does that mean it this will only work from that specific date? If so, is there a way that it will work day to day? Does that make sense? Thanks again.[/QUOTE]It will work for any date but when testing there is no user input so we add a fake input.
3 days before or after the current date[/quote]I'm not sure it's debatable - at least not if one applies the premises upon which the script was built: 3 days before and 3 days after. I think it's a simple logic error.
Most people would say 1 day's time is tommorrow, not the day after...[/quote]If you look 3 calendar days back to 00:00:00 then you should look forward to 23:59:59 - or at least to 00:00:01... Or you look for +/- 3 days ago/hence from the current time. Tomorrow's date remains tomorrow's date until 23:59:59.
[code=php]<?php
// Alternative 1:
$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(23,59,59,date('m'),date('d')+3);
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime) {
echo 'pass';
} else {
echo 'fail';
}
// Alternative 2:
$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(00,00,00,date('m'),date('d')+4);
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime) {
echo 'pass';
} else {
echo 'fail';
}
// Alternative 3:
$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(00,00,00,date('m'),date('d')+3);
if($UserTS < $threeDaysAgo or $UserTS >= $threeDaysTime) {
echo 'pass';
} else {
echo 'fail';
}
?>[/code]
If the premise were "72 hours" instead of "+/- 3 calendar days", then the computation and comparison would look different, too.[/QUOTE]
[code=php]$threeDaysTime = mktime(0,0,0,date('m'),date('d')+3);
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime) [/code]
because if today were/is 2007-03-08, then based on the two original mktime(), three days ago would be 2007-03-05 00:00:00 and three days hence would be 2007-03-11 00:00:00. In this case, the condition test would fail because $UserTS would be equal and not less than 2007-03-05 00:00:00 - although it should pass because the date is not outside the allowable 6-day range.[code=php]if($UserTS >= $threeDaysAgo or $UserTS <= $threeDaysTime)[/code]
I think... because it's not late here anymore... it's now very early...I think... because it's not late here anymore... it's now very early...[/QUOTE]You are really starting to confuse me. I did a test on the code I posted right at the start of the thread and it works just fine. Run the following (which uses the code from that post) to see if we are talking about the same thing because I am sure we are at cross purposes
[code=php]<?php
$UserDate = date('Y-m-d', time());
$UserTS = strtotime($UserDate);
$FiveDaysEarlier = $UserTS - (60*60*24*5);
$FiveDaysLater = $UserTS + (60*60*24*5);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(0,0,0,date('m'),date('d')+3);
$i = -5;
while($FiveDaysEarlier <= $FiveDaysLater)
{
@$content .= '<p><span>' . when($i++).': </span>';
if($FiveDaysEarlier < $threeDaysAgo or $FiveDaysEarlier > $threeDaysTime)
{
$content .= '<span class="green">pass</span></p>';
}
else
{
$content .= '<span class="red">fail</span></p>';
}
$FiveDaysEarlier += (60*60*24); // add a day
}
header('Content-Type: text/html; charset=ISO-8859-1');
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<style type="text/css">
p{color:black;font-weight:bold;clear:both;}
.red{color:red;}
.green{color:green;}
p span{float:left;width:7em}
</style>
<title>Dates</title>
</head>
<body>
<?php echo $content ?>
</body>
</html><?php
function when($i)
{
if(1 < ($j=abs($i)))
{
return "$j days ".($i==$j?'time':'ago');
}
elseif($i)
{
return ($i==$j?'Tomorrow':'Yesterday');
}
else
{
return 'Today';
}
}
?>[/code]
[code=php]<?php
$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(0,0,0,date('m'),date('d')+3);
echo "User Date: " . date("Y-m-d H:i:s", $UserTS) . "<br>";
echo "3 Days Ago: " . date("Y-m-d H:i:s", $threeDaysAgo) . "<br>";
echo "3 Days Hence: " . date("Y-m-d H:i:s", $threeDaysTime) . "<br>";
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime) {
echo 'Test1: pass';
} else {
echo 'Test1: fail';
}
echo "<br>";
if($UserTS <= $threeDaysAgo or $UserTS >= $threeDaysTime) {
echo 'Test2: pass';
} else {
echo 'Test2: fail';
}
?>[/code]
Test1, the original, is correct. Test2 is correct.[code=php]<?php
$UserDate = '2007/03/05';
$UserTS = strtotime($UserDate);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(0,0,0,date('m'),date('d')+3);
echo "User Date: " . date("Y-m-d H:i:s", $UserTS) . "<br>";
echo "3 Days Ago: " . date("Y-m-d H:i:s", $threeDaysAgo) . "<br>";
echo "3 Days Hence: " . date("Y-m-d H:i:s", $threeDaysTime) . "<br>";
if($UserTS < $threeDaysAgo or $UserTS > $threeDaysTime) {
echo 'Test1: pass';
} else {
echo 'Test1: fail';
}
echo "<br>";
if($UserTS <= $threeDaysAgo or $UserTS >= $threeDaysTime) {
echo 'Test2: pass';
} else {
echo 'Test2: fail';
}
?>[/code]
Test1, the original, is incorrect. Test2 is correct.at least 3 days before or after the current date[/QUOTE]To me that means "[I]more than[/I]" which would make the behaviour of example one correct.
Two things: (1) there was no logic error as my example (post #11) demonstrates and (2) what is your understanding of:To me that means "[I]more than[/I]" which would make the behaviour of example one correct.
So... people that believe "[I]at least three days[/I]" means "[I]more than two[/I]" can use [B]<=[/B] and [B]>=[/B], and people that believe "[I]at least three days[/I]" means "[I]more than three[/I]" can use [B]<[/B] and [B]>[/B].
What do you think?[/QUOTE]
...that takes a date from user input and determines if it is at least 3 days before or after the current date.[/quote]I think that the keyword in the OP was "date", as opposed to "72 hours from now". As such we need to compare dates and a date runs from 00:00:00 to 23:59:59, [B]INCLUSIVE[/B]. In this case, this is where the "logic" fails, as your condition test excludes 00:00:00 on the past end and doesn't consider 23:59:59 as part of the future date.
As such we need to compare dates and a date runs from 00:00:00 to 23:59:59, [B]INCLUSIVE[/B]. In this case, this is where the "logic" fails, as your condition test excludes 00:00:00 on the past end and doesn't consider 23:59:59 as part of the future date.[/QUOTE]I don't understand this! A date is a date; it has no concept of time. Time is only needed if we want to break a date into smaller units. As far as a logic error goes did you run
[code=php]5 days ago (2007-03-04):pass -> should be: fail
4 days ago (2007-03-05):pass -> should be: fail
3 days ago (2007-03-06):fail -> should be: pass
2 days ago (2007-03-07):fail -> should be: pass
Yesterday (2007-03-08):fail -> should be: pass
Today (2007-03-09):fail -> should be: pass
Tomorrow (2007-03-10):fail -> should be: pass
2 days time (2007-03-11):fail -> should be: pass
3 days time (2007-03-12):fail -> should be: pass
4 days time (2007-03-13):pass -> should be: fail
5 days time (2007-03-14):pass -> should be: fail[/code]
You have to invert the condition tests.[code=php]<?php
$UserDate = date('Y-m-d', time());
$UserTS = strtotime($UserDate);
$FiveDaysEarlier = $UserTS - (60*60*24*5);
$FiveDaysLater = $UserTS + (60*60*24*5);
$threeDaysAgo = mktime(0,0,0,date('m'),date('d')-3);
$threeDaysTime = mktime(0,0,0,date('m'),date('d')+3);
$i = -5;
while($FiveDaysEarlier <= $FiveDaysLater)
{
@$content .= '<p><span>' . when($i++).': </span>';
if($FiveDaysEarlier >= $threeDaysAgo or $FiveDaysEarlier <= $threeDaysTime)
{
$content .= '<span class="green">pass</span></p>';
}
else
{
$content .= '<span class="red">fail</span></p>';
}
$FiveDaysEarlier += (60*60*24); // add a day
}
header('Content-Type: text/html; charset=ISO-8859-1');
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<style type="text/css">
p{color:black;font-weight:bold;clear:both;}
.red{color:red;}
.green{color:green;}
p span{float:left;width:7em}
</style>
<title>Dates</title>
</head>
<body>
<?php echo $content ?>
</body>
</html><?php
function when($i)
{
if(1 < ($j=abs($i)))
{
return "$j days ".($i==$j?'time':'ago');
}
elseif($i)
{
return ($i==$j?'Tomorrow':'Yesterday');
}
else
{
return 'Today';
}
}
?> [/code]
I'm sure you're on a wind-up...[/quote]Holy cow!
Bordom is the bringer of unrest, leads to disruption.[/QUOTE]I thrive on boredom. Unrest and disruption are my specialties. PHP is only a tool to that end...
Bordom is the bringer of unrest, leads to disruption.[/QUOTE]I've always believed boredom is the step before original thought and creativity.
I've always believed boredom is the step before original thought and creativity.[/QUOTE]Then I must be... No, never mind... ?
0.1.9 — BETA 6.1