/    Sign up×
Community /Pin to ProfileBookmark

need help with date compare

I’m trying to retrieve an expiration date from a database and compare it to todays date. If the expiration date hasn’t arrived yet, I want to execute the below HTML program, if not I want to display a message. I don’t know how to incorporate the HTML. ?

[code=php]<?php
mysql_connect(localhost,root,””);
mysql_select_db(expiredb) or die( “Unable to select database”);
if(!empty($_POST[“submit”]))
{
$noticedate = $_POST[‘noticedate’];
$expiredate = $_POST[‘expiredate’];
$query=”SELECT noticedate,expiredate FROM entrydata”;
$result=mysql_query($query);
if(mysql_num_rows($result))
{
$todays_date = date(“Y-m-d”);
$today = strtotime($todays_date);
$expiration_date = strtotime($expiredate);
if ($expiration_date > $today) echo “Time left”;
else echo “Time elapsed”;
?>[/code]

[code=html]<HTML><HEAD>
</head>
<frameset rows=”25%,75%” frameborder=”0″ border=”0″ framespacing=”0″>
<frame src=”hologo.html” name=”Logo” marginwidth=”0″ marginheight=”0″ noresize scrolling=”no”>
<frameset cols=”170,*” frameborder=”0″ border=”0″ framespacing=”0″>
<frame src=”honav.html” name=”Navigation” noresize scrolling=”auto” marginwidth=”0″ marginheight=”0″>
<frame src=”hocalendar.html” name=”Main” noresize scrolling=”auto” marginwidth=”0″ marginheight=”0″>
</frameset>
<noframes>
<em>Sorry, your browser does not support frames.</em>
To visit our unframed site <a href=”hoinfo.html”>click here</a>
</noframes>
</frameset>
</html>[/code]

to post a comment
PHP

26 Comments(s)

Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — If I understand your requirement and assuming your sql query works correctly, one way you could do would be like this:

[code=php]if ($expiration_date < $today) { ?>

<HTML><HEAD>
</head>
<frameset rows="25&#37;,75%" frameborder="0" border="0" framespacing="0">
<frame src="hologo.html" name="Logo" marginwidth="0" marginheight="0" noresize scrolling="no">
<frameset cols="170,*" frameborder="0" border="0" framespacing="0">
<frame src="honav.html" name="Navigation" noresize scrolling="auto" marginwidth="0" marginheight="0">
<frame src="hocalendar.html" name="Main" noresize scrolling="auto" marginwidth="0" marginheight="0">
</frameset>
<noframes>
<em>Sorry, your browser does not support frames.</em>
To visit our unframed site <a href="hoinfo.html">click here</a>
</noframes>
</frameset>
</html>

<?php }
else echo "Time left";[/code]
Copy linkTweet thisAlerts:
@eval_BadCode_Oct 26.2010 — depending on the types the dates are in mysql (int/varchar/date) you can simply do this

$query="

SELECT noticedate,expiredate FROM entrydata

WHERE (expiredate-NOW())>=0";

That is how to select all of the records where the expiration date has not yet been reached. If you want to select the time left you can use more mysql date functions, I personally like using YEAR(expiredate) MONTH(expiredate) and DAY(expiredate) even though it can make for a messy query.

w3cschools has an excellent section on mysql date and time functions, its very very short and to the point. I suggest looking at it.

For future reference, dates can be compared like numbers.

Maybe this is what you want:

$query="

SELECT noticedate,expiredate,(expiredate-NOW()) as timeleft FROM entrydata

WHERE (expiredate-NOW())>0";
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — I'm only concerned with time, not what's in the records. I only want the program to work until the expiration date has been reached.
Copy linkTweet thisAlerts:
@eval_BadCode_Oct 26.2010 — I'm only concerned with time, not what's in the records. I only want the program to work until the expiration date has been reached.[/QUOTE]

if (mysql_num_rows($resource) == 0) {

die('There were no records with expiration dates past today's date');

}

edit: never-mind you already got that.
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — I got this error:
[CODE]Parse error: syntax error, unexpected $end in C:xampphtdocshofileshoexpire.php on line 38[/CODE]
when I executed this code:
[code=php]<?php
mysql_connect(localhost,root,"");
mysql_select_db(expiredb) or die( "Unable to select database");
if(!empty($_POST["submit"]))
{
$noticedate = $_POST['noticedate'];
$expiredate = $_POST['expiredate'];
$query="SELECT noticedate,expiredate FROM entrydata";
$result=mysql_query($query);
if(mysql_num_rows($result))
{
$todays_date = date("Y-m-d");
$today = strtotime($todays_date);
$expiration_date = strtotime($expiredate);
if ($expiration_date > $today) {
?>[/code]

[code=html]<HTML><HEAD>
</head>
<frameset rows="25&#37;,75%" frameborder="0" border="0" framespacing="0">
<frame src="hologo.html" name="Logo" marginwidth="0" marginheight="0" noresize scrolling="no">
<frameset cols="170,*" frameborder="0" border="0" framespacing="0">
<frame src="honav.html" name="Navigation" noresize scrolling="auto" marginwidth="0" marginheight="0">
<frame src="hocalendar.html" name="Main" noresize scrolling="auto" marginwidth="0" marginheight="0">
</frameset>
<noframes>
<em>Sorry, your browser does not support frames.</em>
To visit our unframed site <a href="hoinfo.html">click here</a>
</noframes>
</frameset>
</html>[/code]

[code=php]]<?php
}
else{
echo "Time elapsed";
}
?>
[/code]
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — You have a square bracket paste in your code in this thread. Is that in your php flle as well?

Also, your solution is to be contained all in 1 file (your PHP script file) and not separated into 2 files -- PHP and HTML. Sorry if I did not make that clear.

And you have a number of curlies started in your PHP. Make sure you close them off at the end of the script.
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — You have a square bracket paste in your code in this thread. Is that in your php flle as well?

Also, your solution is to be contained all in 1 file (your PHP script file) and not separated into 2 files -- PHP and HTML. Sorry if I did not make that clear.[/QUOTE]


I'm sorry but now I'm lost. The square bracket is not in the code. And are you saying I can't incorporate the HTML into the PHP program? The idea is to execute the html if the conditions are met.
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — (1) In your recent post you have a "PHP Code" section, then an "HTML Code" section and finally another "PHP Code" section. I want to make sure you have each of these 3 components of code contained in a single web document. Otherwise coding technique won't work.

(2) the square bracket "]" I was referring too appears in the last section of PHP code. Not sure if you have it in your file or it was just an inadvertent key stroke during paste, but it does not belong.

(3) and in your upper PHP section you have 2 open curlies "{" that you need to be sure you close off in the last section of PHP code.

Hope that helps.
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — OK, Yes my third thumb created the extra [B]][/B], the program is coded:

<?php

mysql_connect(localhost,root,"");

mysql_select_db(expiredb) or die( "Unable to select database");

if(!empty($_POST["submit"]))

{

$noticedate = $_
POST['noticedate'];

$expiredate = $_POST['expiredate'];

$query="SELECT noticedate,expiredate FROM entrydata";

$result=mysql_query($query);

if(mysql_num_rows($result))

{

$todays_date = date("Y-m-d");

$today = strtotime($todays_date);

$expiration_date = strtotime($expiredate);

if ($expiration_date > $today) {

?>

<HTML><HEAD>

</head>

<frameset rows="25&#37;,75%" frameborder="0" border="0" framespacing="0">

<frame src="hologo.html" name="Logo" marginwidth="0" marginheight="0" noresize scrolling="no">

<frameset cols="170,*" frameborder="0" border="0" framespacing="0">

<frame src="honav.html" name="Navigation" noresize scrolling="auto" marginwidth="0" marginheight="0">

<frame src="hocalendar.html" name="Main" noresize scrolling="auto" marginwidth="0" marginheight="0">

</frameset>

<noframes>

<em>Sorry, your browser does not support frames.</em>

To visit our unframed site <a href="hoinfo.html">click here</a>

</noframes>

</frameset>

</html>

<?php

}

else{

echo "Time elapsed";

}

?>
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — Yes, that's right. Now you just need to address the imbalance of curlies "{}". You have one too many unclosed curlies originating in your upper section of code. It's the one dealing with the condition when your _POST['submit'] is empty().
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — thanks so much for your help. Now no errors, just a blank page ? My expiration date is [B]2010-10-28[/B]
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — Ok, copy and paste your current code so we can have a look.

And, throw this in at the beginning of your PHP Script (and in any script going forward). It helps you to see warnings and errors that may be hidden due to php.config and server-side settings out of the box. You can always comment this out at time of go-live:

error_reporting(E_ALL); // show all errors - server configured at 6135, increases to 6143 (PHP version 5.2.11)

ini_set('display_errors','On'); // turn on error display
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — <?php

mysql_connect(localhost,root,"");

mysql_select_db(expiredb) or die( "Unable to select database");

if(!empty($_POST["submit"]))

{

$noticedate = $_
POST['noticedate'];

$expiredate = $_POST['expiredate'];

$query="SELECT noticedate,expiredate FROM entrydata";

$result=mysql_query($query);

if(mysql_num_rows($result))

{

$todays_date = date("Y-m-d");

$today = strtotime($todays_date);

$expiration_date = strtotime($expiredate);

if ($expiration_date > $today) {

?>

<HTML><HEAD>

</head>

<frameset rows="25&#37;,75%" frameborder="0" border="0" framespacing="0">

<frame src="hologo.html" name="Logo" marginwidth="0" marginheight="0" noresize scrolling="no">

<frameset cols="170,*" frameborder="0" border="0" framespacing="0">

<frame src="honav.html" name="Navigation" noresize scrolling="auto" marginwidth="0" marginheight="0">

<frame src="hocalendar.html" name="Main" noresize scrolling="auto" marginwidth="0" marginheight="0">

</frameset>

<noframes>

<em>Sorry, your browser does not support frames.</em>

To visit our unframed site <a href="hoinfo.html">click here</a>

</noframes>

</frameset>

</html>

<?php

}

else{

echo "Time elapsed";

}

}// end of it statement for 'results found'

}// end of if statement for no 'submit'

?>
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — You probably got the white page as a result of your submit button test.

Make this little change to your script for testing purposes:

}// end of it statement for 'results found'

[COLOR="Red"]echo '_POST["submit"] is actually empty.';[/COLOR]

}// end of if statement for no 'submit'
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — i got the same results
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — i got the same results[/QUOTE]

What, a white page? Did you turn on error display? And still a white page?

Can you reveal the URL (or PM me with it.)
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — the url is localhost mainpage (hammockoaks.html) which works without the script we're working on. the code for that is in the script. the current code has been posted, including your latest suggestion [CODE]echo '_POST["submit"] is actually empty.';
[/CODE]
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — ? I would like to know the full URL (include domain) so I can walk through it myself.
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — this is a local in-house (localhost) system
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — No, that doesn't do it. I was hoping to see your submit form and walk through the process. It is going to be hard for me to troubleshoot. Are you able to use "echoes" to put check points into your script and walk through and debug yourself at this point?

Or can you post a walk through scenario with (a) your expectations and (b) the actual results?
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — Are you able to use "echoes" to put check points into your script and walk through and debug yourself at this point? - I'm just learning PHP and no I don't know how.

Or can you post a walk through scenario with (a) your expectations and (b) the actual results? - I want to key in "thisname.php" and depending on the [B]expdate [/B]stored in the database, either execute the program or echo an alert. At this point when I execute the program all I get is a blank page. There is no form being used.
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — I want to key in "thisname.php" and depending on the [B]expdate [/B]stored in the database, either execute the program or echo an alert. At this point when I execute the program all I get is a blank page. There is no form being used.[/QUOTE]

Ok, I think this is a bit bigger/different than what was initially posted.

Let me see if I understand what you are after -- are you saying you want to be able to enter "thisname.php" in your browser's address bar and determine is the "web document" has expired or not?

Is that sort of what you are trying to do here?

If not, take another shot at explaining please.
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — Oh if I had your patience - please bear with me, I'm not that articulate; All this is as much a learning exercise (PHP) as anything. I have developed this system (particular to an application), and someone wants to use it for a year. I have the expiration date stored in a database. I want to at the end 11 months, echo an alert that there is one month left to use this. At the end of the year I want to make it unusable.
Copy linkTweet thisAlerts:
@devel95Oct 26.2010 — Ok.

I think your requirements might be complex. We could easily chew up this forum thread with a lot of back & forth posts -- but I feel that based on your initial issue, I have offered a solution that other forum users might find useful.

So I would like to suggest that rather than bogging down the forum with an application development dialogue between you and I, can you please PM ("Private Message") me and I will respond to ask you some more questions that can help me best offer a practical course of action for you and your app.

I think we should take this offline at this time.
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — sounds like a plan
Copy linkTweet thisAlerts:
@ckdoublenecksauthorOct 26.2010 — would this approach work?

<?php

mysql_connect(localhost,root,"");

mysql_select_db(expiredb) or die( "Unable to select database");

if(!empty($_POST["submit"]))

{

$noticedate = $_
POST['noticedate'];

$expiredate = $_POST['expiredate'];

$query="SELECT noticedate,expiredate FROM entrydata";

$result=mysql_query($query);

if(mysql_num_rows($result))

{

$todays_date = date("Y-m-d");

$today = strtotime($todays_date);

$expiration_date = strtotime($expiredate);

if ($expiration_date > $today) {

header("Location: hammockoaks.html");

exit;


}

elseif ($expiration_date = $today) {

header("Location: hologo.html");

}else {

}

}// end of it statement for 'results found'

echo '_POST["submit"] is actually empty.';

}// end of if statement for no 'submit'

?>
×

Success!

Help @ckdoublenecks 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 5.8,
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,
)...