Date Text Problem -- New to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

Date Text Problem -- New to PHP

Post by mvd7793 »

I don't know PHP, but I was told that I could come and do what I am trying to do in PHP. I need to make text show on a set date, such as Friday, August 18, 2006. The text then needs to stay there normal. For example,


The visitor clicks a link that says, click here for 8/19/06's random fact. Then, if the date is before then, it shows "You can't see that! It's not time for it yet". If it is that date or after, then the text shown is "The monkey's Latin name rhymes with cat".

Can someone give me a script that does that?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

have a link on your page like this

Code: Select all

echo '<a href="somePage.php?date='.strtotime("8/19/06").'>click here for 8/19/06\'s random fact</a>';
Then on somePage.php

Code: Select all

$date = $_GET['date'];

if($date < time())
   echo "You can't see that! It's not time for it yet";
else
   echo 'The monkey\'s Latin name rhymes with cat';
NOTE: Untested
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Code: Select all

<?php
$text = "The monkey's Latin name rhymes with cat";
$month = 8;
$day = 17;
$year = 2006;

$date = mktime(0, 0, 0, $month, $day, $year);
$day_after_date = $date + (60 * 60 * 24);
$now = time();

if( $now >= $date){
	if($now <= $day_after_date){
    	echo $text;
	}
	else{
		echo "You are too late!";
	}
}
else{
    echo "It is not time for you to see this yet";
}
?>
not tested
uhh... yea his is better. :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mvd7793 wrote:Can someone give me a script that does that?
Image, give you a script? No. Help you make it yourself? Yes. Learning is an goal we must do ourselves, not be dragged to.

I would use date() in two places: the in-page displayed date and the date passed via URL when requesting the "random fact."

I would choose to use the format of YYYYMMDD or some combination thereof. As long as it doesn't have symbols, it'll work with this method. Now, on the fact request page, I would look at the date passed. Is it all numbers? ctype_digit(). Is it the right length? strlen().

After this bit of checking it's time to break out substr() and extract out the year, month and day. It's probably a good idea to use checkdate() to make sure they are valid. Now give them to mktime(), then date() to format them for lookup: YYYY-MM-DD. We do this because the date given can actually be off in a few ways whereas mktime() will correct it. Now it's just a matter of looking this new date up somewhere after verifying that the date is not in the future by using time() and comparing the return value from mktime().
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

I'm Trying it

Post by mvd7793 »

Ok, I'm trying it. Please stay tuned for any other responses from me.
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

What do I make the PHP Page in?

Post by mvd7793 »

Can I make the PHP page in Notepad?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yes Image

But check here for better (free) editors that will make your life easier
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

Ok

Post by mvd7793 »

OK. Thanks
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

Couldn't Get It to Work

Post by mvd7793 »

I couldn't get it to work. Do I have to put the php code in an HTML page, or a PHP page? Do I have to put any special tage around the PHP code?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Read the manual - http://www.php.net/manual/en/tutorial.firstpage.php

P.S. Dont PM me Image
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

Ok

Post by mvd7793 »

Ok. I will look at it and see if I can figure it out.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Couldn't Get It to Work

Post by RobertGonzalez »

mvd7793 wrote:I couldn't get it to work. Do I have to put the php code in an HTML page, or a PHP page? Do I have to put any special tage around the PHP code?
This leads me to believe that you have limited PHP development experience. There is nothing wrong with that, but I would realy recommend you read the introduction to PHP portion of the PHP Manual. Then click the links in the table of contents and read through them.

After that, read the Basic tutorial section of the PHP Manual. Click the links in the table of contents and read throught those articles. Practise what you learn there, then try some stuff, then come back here and post with any problems you may be having.
mvd7793
Forum Newbie
Posts: 7
Joined: Fri Aug 18, 2006 9:13 am

I am new...

Post by mvd7793 »

I am new to php. I have never done it. I really don't have time to learn it. Can someone please give me a code, fully ready for an html or php page? You could also take one of the ones above and put some simple brackets around it. I really don't have a clue how to do that.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There are plenty of developers that will write this code for you... for a fee. Few around here will do it for you.
Post Reply