/    Sign up×
Community /Pin to ProfileBookmark

Date Comparison

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.

to post a comment
PHP

23 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 07.2007 — [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]
Copy linkTweet thisAlerts:
@NightShift58Mar 07.2007 — [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...?
Copy linkTweet thisAlerts:
@geetungauthorMar 07.2007 — 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.
Copy linkTweet thisAlerts:
@bokehMar 07.2007 — [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.
Copy linkTweet thisAlerts:
@NightShift58Mar 07.2007 — 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.

Your strttotime() is using (by default) "2007/03/25 00:00:00" and as such, you would only "pass" for 2 days or less and not for the third day, al no superar el "00:00:00".

If the premise were "72 hours" instead of "+/- 3 calendar days", then the computation and comparison would look different, too.
Copy linkTweet thisAlerts:
@NightShift58Mar 07.2007 — Three correct alternatives to explain what I'm trying to say:[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]
Copy linkTweet thisAlerts:
@bokehMar 08.2007 — Too late at night. Strtotime is not well documented and produces all sorts of strange results. For example: [B]strtotime('3 days')[/B] and [B]strtotime('3 days ago')[/B] retain the current hour. The only reason I used it here is because I didn't know the format of the date.
Copy linkTweet thisAlerts:
@bokehMar 08.2007 — What's happened to the "edit" button? By the way the post above relates to this: If the premise were "72 hours" instead of "+/- 3 calendar days", then the computation and comparison would look different, too.[/QUOTE]
Copy linkTweet thisAlerts:
@NightShift58Mar 08.2007 — Used as it is in the snippet, it would always return "2007-03-05 00:00:00" and that would be okay.

Problems arise with this:[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.

The same thing can happen at the other end of the spectrum.

Add to that the fact that the "<" and ">" are applied to the wrong variables and chaos is perfect.

So, to keep the triple zeroes for h-m-s in mktime(), one would have to invert the condition test as well as test for equality:[code=php]if($UserTS >= $threeDaysAgo or $UserTS <= $threeDaysTime)[/code]I think... because it's not late here anymore... it's now very early...
Copy linkTweet thisAlerts:
@bokehMar 08.2007 — 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]
Copy linkTweet thisAlerts:
@NightShift58Mar 08.2007 — Link: http://www.nightshift58.com/webdev/test_bokeh_time.php[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.
Copy linkTweet thisAlerts:
@NightShift58Mar 08.2007 — Sorry... No Edit Button anymore

Link: http://www.nightshift58.com/webdev/test_bokeh_time.php[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.
Copy linkTweet thisAlerts:
@bokehMar 08.2007 — Two things: (1) there was no logic error as my example (post #11) demonstrates and (2) what is your understanding of: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.

I think this is more a question interpretation of the original question than a matter of coding practice.

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?
Copy linkTweet thisAlerts:
@NightShift58Mar 08.2007 — 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]

Setting aside the time zone issue, I think that if we said "Tomamos un café mañana", it would definitely mean the same as "Let's have coffee on 2007-03-09 between 00:00:00 and 23:59:59".

If we apply that to "Let's have coffee in the 3 days after today", it would open the window of coffee opportunity from "2007-03-09 00:00:00" through "2007-03-11 23:59:59"....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.

But what I [B]really[/B] think is that this is as close as we get to a dead horse and the OP doesn't want to ride it anyways ?

Even if we come to "agreement" on the meaning of "until..." in this thread, the next exercise would open up the discussion up again, depending on the wording.

In a program spec sheet, obviously, from- and to-dates would be defined much differently because these problems always come up.
Copy linkTweet thisAlerts:
@bokehMar 08.2007 — 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 my code? The result is completely balanced with three days in either direction.
Copy linkTweet thisAlerts:
@NightShift58Mar 08.2007 — I did and you're right... It's balanced... but wrong...

You're failing where you should pass and passing where you should fail.
[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.
Copy linkTweet thisAlerts:
@NightShift58Mar 08.2007 — Specifically, your code should look like this:[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]
Copy linkTweet thisAlerts:
@bokehMar 08.2007 — I'm sure you're on a wind-up, he only wants dates that are more than three days away, i.e. ot too close to the current day. That's my interpretation anyway.
Copy linkTweet thisAlerts:
@SheldonMar 08.2007 — Bokeh's code is correct as far as any one would have any real use for. And it is correct in the sense that it suited at the correct answer to the original question. Maybe it is you NightShade58 that has to much time on your hands, Bordom is the bringer of unrest, leads to disruption.
Copy linkTweet thisAlerts:
@NightShift58Mar 09.2007 — I'm sure you're on a wind-up...[/quote]Holy cow!

You're right - that's what it says down there.

I read it 10-20 times - if not more - and always interpreted it as being a range of "from 3 days ago to 3 days hence". I read it again and now it make perfect sense - even if I can't think of a real-life situation for it.

My sincerest, humblest apologies.

I'll probably do it again, though. My way of keeping you sharp...

No, really, I'm sorry. This is embarrassing, actually. But you got to give me credit, though, I can pontificate like the best...
Copy linkTweet thisAlerts:
@NightShift58Mar 09.2007 — 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...
Copy linkTweet thisAlerts:
@bokehMar 09.2007 — Bordom is the bringer of unrest, leads to disruption.[/QUOTE]I've always believed boredom is the step before original thought and creativity.
Copy linkTweet thisAlerts:
@NightShift58Mar 09.2007 — I've always believed boredom is the step before original thought and creativity.[/QUOTE]Then I must be... No, never mind... ?
×

Success!

Help @geetung spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...