PHP - Time of day script

  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 8th, 2003, 9:31 pm

Hey you PHP programmer you,

I need to include a PHP script on the bottom of my page that:

if time is 10:00 PM - 8:00 AM, then display banner.htm

if not, then display nothing.


any help?
feel free to ask any questions.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post November 8th, 2003, 9:31 pm

  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post November 8th, 2003, 9:48 pm

yay boredom buster! thx!!!

Code: [ Select ]
<?

$current_time = date(G);

if ($current_time >= 22 || $current_time < 8) {
  echo ('<img src="image.jpg">');
}

?>
  1. <?
  2. $current_time = date(G);
  3. if ($current_time >= 22 || $current_time < 8) {
  4.   echo ('<img src="image.jpg">');
  5. }
  6. ?>


Remember, since PHP is server side the script runs according to the server time, not the client's. It would be easier to do it in javascript if you wanted to set it up so it was according to clients. You could also offset it so it was set up to be the right time for you.
Pixel Acres V2
  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 8th, 2003, 9:55 pm

well, this is hte purpose.

I want banner ads to appear on the bottom of my page at night time, not day time.

Would I be able to do it in javascript and have the actual HTM code be in javascript too?

Ok, here is the code I want to be displayed at night:

Code: [ Select ]
<table width="500" border="0" cellspacing="0" cellpadding="0">
 
 <tr>
  <td><a href="http://www.BannersGoMLM.com/cgi-bin/ads/left/ads_nonssi.pl?advert=NonSSI&page=willstein.146866" target="_blank">
   <img src="http://www.BannersGoMLM.com/cgi-bin/ads/left/ads_nonssi.pl?ID=bgmlm&page=willstein.146866" width="250" height="72" border="0"></a></td>
  <td><a href="http://www.BannersGoMLM.com/cgi-bin/ads/right/ads_nonssi.pl?advert=NonSSI&page=willstein.146866" target="_blank">
   <img src="http://www.BannersGoMLM.com/cgi-bin/ads/right/ads_nonssi.pl?ID=bgmlm&page=willstein.146866" width="250" height="72" border="0"></a></td>
 </tr>
 <tr>
  <td colspan="2"><a href="http://BannersGoMLM.com/cgi-bin/ref/index.cgi?dist=willstein" target="_blank">
   <img src="http://BannersGoMLM.com/images/codebottom.gif" width="500" height="19" border="0"></a></td>
 </tr>
</table>


<div align="left">
 <SCRIPT LANGUAGE="JavaScript">
banner = '<TABLE CELLSPACING=0 CELLPADDING=0><TR><TD><A HREF="http://www.1800banners.com/cgi-bin/bpwork2.cgi?advert=NonSSI&page=44">'
banner += '<IMG SRC="http://www.1800banners.com/cgi-bin/bpwork2.cgi?ID=willstein&page=44" ALT="1800Banners.com – Free Banner Exchange" BORDER=0 width=468 height=60></a></TD>'
banner += '</TR><TR><TD><A HREF="http://www.1800banners.com"><IMG BORDER=0 SRC="http://www.1800banners.com/logo.gif" ALT="1800Banners.com – Free Banner Exchange"></A></TD></TR></TABLE>'
document.write(banner)
// -->
</SCRIPT>
 <NOSCRIPT>
 <TABLE CELLSPACING=0 CELLPADDING=0>
  <TR>
   <TD><A HREF="http://www.1800banners.com/cgi-bin/bpwork2.cgi?advert=NonSSI&page=44"><IMG SRC="http://www.1800banners.com/cgi-bin/bpwork2.cgi?ID=willstein&page=44" ALT="1800Banners.com – Free Banner Exchange" BORDER=0 width=468 height=60></a></TD>
  </TR>
  <TR>
   <TD><A HREF="http://www.1800banners.com"><IMG BORDER=0 SRC="http://www.1800banners.com/logo.gif" ALT="1800Banners.com – Free Banner Exchange"></A></TD>
  </TR>
 </TABLE>
 </NOSCRIPT>
</div>
<iframe src="http://rfbe.com/iframe/?id=1793&bn=1&pg=1295" width=468 height=60 marginwidth=0 marginheight=0 scrolling=no frameborder=0><a href="http://rfbe.com/href/?id=1793&bn=1&pg=1295" target="_top"><img src="http://rfbe.com/banner/?id=1793&bn=1&bt=0&pg=1295" alt="Relmax Flash Banner Exchange" width=468 height=60 border=0></a></iframe>
<p> </p>
<div align="left">
 <iframe src="http://leader.linkexchange.com/X1720316/showiframe?" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no>
 <a href="http://leader.linkexchange.com/X1720316/clickle" target="_top"><img width=468 height=60 border=0 ismap alt="" src="http://leader.linkexchange.com/X1720316/showle?"></a></iframe>
 <br>
 <a href="http://leader.linkexchange.com/X1720316/clicklogo" target="_top"><img src="http://leader.linkexchange.com/X1720316/showlogo?" width=468 height=16 border=0 ismap alt=""></a><br>
</div>
  1. <table width="500" border="0" cellspacing="0" cellpadding="0">
  2.  
  3.  <tr>
  4.   <td><a href="http://www.BannersGoMLM.com/cgi-bin/ads/left/ads_nonssi.pl?advert=NonSSI&page=willstein.146866" target="_blank">
  5.    <img src="http://www.BannersGoMLM.com/cgi-bin/ads/left/ads_nonssi.pl?ID=bgmlm&page=willstein.146866" width="250" height="72" border="0"></a></td>
  6.   <td><a href="http://www.BannersGoMLM.com/cgi-bin/ads/right/ads_nonssi.pl?advert=NonSSI&page=willstein.146866" target="_blank">
  7.    <img src="http://www.BannersGoMLM.com/cgi-bin/ads/right/ads_nonssi.pl?ID=bgmlm&page=willstein.146866" width="250" height="72" border="0"></a></td>
  8.  </tr>
  9.  <tr>
  10.   <td colspan="2"><a href="http://BannersGoMLM.com/cgi-bin/ref/index.cgi?dist=willstein" target="_blank">
  11.    <img src="http://BannersGoMLM.com/images/codebottom.gif" width="500" height="19" border="0"></a></td>
  12.  </tr>
  13. </table>
  14. <div align="left">
  15.  <SCRIPT LANGUAGE="JavaScript">
  16. banner = '<TABLE CELLSPACING=0 CELLPADDING=0><TR><TD><A HREF="http://www.1800banners.com/cgi-bin/bpwork2.cgi?advert=NonSSI&page=44">'
  17. banner += '<IMG SRC="http://www.1800banners.com/cgi-bin/bpwork2.cgi?ID=willstein&page=44" ALT="1800Banners.com – Free Banner Exchange" BORDER=0 width=468 height=60></a></TD>'
  18. banner += '</TR><TR><TD><A HREF="http://www.1800banners.com"><IMG BORDER=0 SRC="http://www.1800banners.com/logo.gif" ALT="1800Banners.com – Free Banner Exchange"></A></TD></TR></TABLE>'
  19. document.write(banner)
  20. // -->
  21. </SCRIPT>
  22.  <NOSCRIPT>
  23.  <TABLE CELLSPACING=0 CELLPADDING=0>
  24.   <TR>
  25.    <TD><A HREF="http://www.1800banners.com/cgi-bin/bpwork2.cgi?advert=NonSSI&page=44"><IMG SRC="http://www.1800banners.com/cgi-bin/bpwork2.cgi?ID=willstein&page=44" ALT="1800Banners.com – Free Banner Exchange" BORDER=0 width=468 height=60></a></TD>
  26.   </TR>
  27.   <TR>
  28.    <TD><A HREF="http://www.1800banners.com"><IMG BORDER=0 SRC="http://www.1800banners.com/logo.gif" ALT="1800Banners.com – Free Banner Exchange"></A></TD>
  29.   </TR>
  30.  </TABLE>
  31.  </NOSCRIPT>
  32. </div>
  33. <iframe src="http://rfbe.com/iframe/?id=1793&bn=1&pg=1295" width=468 height=60 marginwidth=0 marginheight=0 scrolling=no frameborder=0><a href="http://rfbe.com/href/?id=1793&bn=1&pg=1295" target="_top"><img src="http://rfbe.com/banner/?id=1793&bn=1&bt=0&pg=1295" alt="Relmax Flash Banner Exchange" width=468 height=60 border=0></a></iframe>
  34. <p> </p>
  35. <div align="left">
  36.  <iframe src="http://leader.linkexchange.com/X1720316/showiframe?" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no>
  37.  <a href="http://leader.linkexchange.com/X1720316/clickle" target="_top"><img width=468 height=60 border=0 ismap alt="" src="http://leader.linkexchange.com/X1720316/showle?"></a></iframe>
  38.  <br>
  39.  <a href="http://leader.linkexchange.com/X1720316/clicklogo" target="_top"><img src="http://leader.linkexchange.com/X1720316/showlogo?" width=468 height=16 border=0 ismap alt=""></a><br>
  40. </div>



Any alternatives besides PHP?
  • mdipi
  • Novice
  • Novice
  • User avatar
  • Joined: Nov 08, 2003
  • Posts: 34
  • Status: Offline

Post November 8th, 2003, 10:06 pm

edit : i had something like what is already posted, only a lot longer and a lot less-optimized. :wink:

Code: [ Select ]
<?php
$time = array("10 PM", "11 PM", "12 AM", "1 AM", "2 AM");
for (i, i < count($time)-1, i++){
if(date("h:A") == $time[i]){
echo <img src="banner.jpg">;
}
?>
  1. <?php
  2. $time = array("10 PM", "11 PM", "12 AM", "1 AM", "2 AM");
  3. for (i, i < count($time)-1, i++){
  4. if(date("h:A") == $time[i]){
  5. echo <img src="banner.jpg">;
  6. }
  7. ?>


the loop is skrewed up though. but thats where i was heading.
  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 8th, 2003, 10:12 pm

lol i keep refreshing the page, and it keeps changing. . . smaller, smaller, smaller.


now its gone.

haha. thats funny. thanks for your help man.

i guess is should be callign for unflux too.
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 10:27 am

Code: [ Select ]
<?

$current_time = date(G);

if ($current_time >= 22 || $current_time < 8) {
   print <<<EOF

   //INSERT ALL CODE BETWEEN HERE

  
   //AND HERE

  EOF;
}

?>
  1. <?
  2. $current_time = date(G);
  3. if ($current_time >= 22 || $current_time < 8) {
  4.    print <<<EOF
  5.    //INSERT ALL CODE BETWEEN HERE
  6.   
  7.    //AND HERE
  8.   EOF;
  9. }
  10. ?>


That will display whatever goes inbetween the two comments from 10PM server time, to 8AM server time. The code can be straight HTML and Javascript ONLY!
Pixel Acres V2
  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 3:40 pm

Hmm, I tried that on my normal, index.htm file, and it didn't seem to work. Does it need to be in a PHP file?

Simply cut and paste that into index.htm as usual, or is there something more involved?

When I include that in a normal html file, it reads:
= 22 || $current_time < 8) { print <<
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8922
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post November 9th, 2003, 5:59 pm

willstein wrote:
Hmm, I tried that on my normal, index.htm file, and it didn't seem to work. Does it need to be in a PHP file?


Yeah it would need to be in a .php file since your server likely has the hanlder for PHP on that type of file, not htm or html extensions.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 6:04 pm

So how would I do this?

I've created an example HTML file (it says, One Two Three Four Five Six Seven Eight Nine).
Code: [ Select ]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ozzu Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p><font color="#FF0000">One Two Three,</font></p>
<p><font color="#FF6600">Four five Six</font></p>
<p><strong><font color="#0000FF">Seven Eight Nine</font></strong></p>
</body>
</html>
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Ozzu Test</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. </head>
  7. <body>
  8. <p><font color="#FF0000">One Two Three,</font></p>
  9. <p><font color="#FF6600">Four five Six</font></p>
  10. <p><strong><font color="#0000FF">Seven Eight Nine</font></strong></p>
  11. </body>
  12. </html>


How would I implement the PHP into it?
Would the homepage have to be, index.php? (therefore assuming that if there is no index.htm, the server will go to index.php?)

Thanks.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post November 9th, 2003, 6:18 pm

You'll need to check with your host as to how they work it. Everyone I've encountered so far provides default "index" or "default" pages. Find out what they require and then just make sure your "index" page will default to it.

For example, on my domain, index.html, index.htm, and default.asp are all default pages for my site, so if I have at least one of them active it will do what I want. And I can add or rremove as I'd like via the Admin controls. Your host should have similar options.

As to implementing PHP into an HTML file, you can't. You need to make it a php file as previously stated and add the HTML into it.
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 6:25 pm

resave index.html as index.php and see if that works...
Pixel Acres V2
  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 7:24 pm

Doesnt work B.

When I change index.htm to index.php, it says:

Parse error: parse error in /home/steinw/public_html/index.php on line 1


The site is:
-REMOVED-

Notice on the bottom:
=22 || $CurHour<8 ) { print 'test'; } ?>
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 7:35 pm

looks like you messed with my variables a bit, so I need to see what the whole script is from your page, from <? to ?>.

It seems like html is parsing something when PHP should be.

Ok this is what you have, it isn't parsing as PHP at all:

Code: [ Select ]
<?PHP

//Current hour of day, this is in the server time
$CurHour = date('H');

//If it's greater than or equal to 22 i.e. 10 or 11PM
//Or if it's less than or equal 08 i.e. 00,01,...,08AM
//Include the banner.htm otherwise do nothing
if ($CurHour>=22 || $CurHour<8) {
print 'test';
}

?>
  1. <?PHP
  2. //Current hour of day, this is in the server time
  3. $CurHour = date('H');
  4. //If it's greater than or equal to 22 i.e. 10 or 11PM
  5. //Or if it's less than or equal 08 i.e. 00,01,...,08AM
  6. //Include the banner.htm otherwise do nothing
  7. if ($CurHour>=22 || $CurHour<8) {
  8. print 'test';
  9. }
  10. ?>


It's nice to see you playing with the code a bit, buuuut

you can't format date with
Code: [ Select ]
date('H');

That will store in variable that makes it much harder because of the leading 0, so I would just stick with G.

What happens if you use just <? instead of <?PHP

Anyways, what is really happening is '<?PHP' starts and HTML considers the '<' as a normal HTML tag, that is why all the code you can see is after the '>' which HTML also interpets as a normal tag end. Have you gotten rid of index.html, so the server isn't going to that one first, instead of index.php?
Pixel Acres V2
  • b_heyer
  • Web Master
  • Web Master
  • User avatar
  • Joined: Jun 15, 2003
  • Posts: 4583
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 7:37 pm

Oh and the parse error is your XML header it has the same <? tags as PHP...
Pixel Acres V2
  • willstein
  • Loser
  • Loser
  • No Avatar
  • Joined: Oct 29, 2003
  • Posts: 117
  • Loc: Maryland
  • Status: Offline

Post November 9th, 2003, 8:14 pm

Hmm,

I dont think i messed with your coding, but if it says that, I must have by mistake.

I'm more confused now, then when I started.
Wanna start with the basics?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post November 9th, 2003, 8:14 pm

Post Information

  • Total Posts in this topic: 21 posts
  • Users browsing this forum: No registered users and 178 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.