简体   繁体   中英

Display content based on date with PHP

I'm putting together a contest site built on Wordpress. Legally, the contest has to start at midnight. To avoid being up at midnight to set up the content, i'd like to build some PHP logic to show everything after the start date, and before then display some basic HTML for a splash page. I'm a TOTAL programming newb, but i'm trying to work the problem out, here's what I have so far:

<?php

// The current date
$date = date('Y, j, m');

// Static contest start date
$contestStart = ('2012, 02, 03');

// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}

else {
// splash content?
}
?>

I feel like there is smarter ways to do this. My site is fairly large... wrapping the whole thing in an if statement seems ridiculous. Maybe a redirect to a different page altogether based on the date?

Any help is appreciated.

You will want to modify your WordPress theme so that the changes can be site-wide.

  1. Log in to your WordPress installation.
  2. Navigate to Appearance > Editor
  3. In the Templates section, go to header.php
  4. At the very beginning of header.php, insert your code :

<?php
$date = time();

$contestStart = strtotime('2012-02-03 00:00:00');

if ($date < $contestStart) {
?>

<html>
    Insert your whole splash page here.
</html>

<?php
exit;
}
?>
//The normal template code should be below here.

Don't forget to click the "Update File" button on WordPress when you're done; and like the others said, make sure that your specified time is synced with whatever timezone the server is in.

I suppose technically your code's logic would work but yes, there are much better ways of doing this. However, for your purpose we will go simple.

You should be comparing against a timestamp and not a string. Try this:

<?php

// The current date
$date = time();

// Static contest start date
$contestStart = strtotime('2012-02-03 00:00:00');

// If current date is after contest start
if ($date > $contestStart) {
//contest content?
}

else {
// splash content?
}
?>

You can put the splash and content components in separate .php files, and simply include() then within the conditionals.

if ($date > $contestStart) {
    include("SECRETDIR/contest.php");
}

else {
    include("SECRETDIR/splash.php");
}

You'll want to set up an .htaccess so that people cannot point their browsers towards secretdir and directly access contest.php

You don't have to wrap the content, you can have something like:

<?php 

if ( time() < strtotime('2012-02-03 00:00:00') ) {
    echo "not yet!";
    exit;
}

//code here wont be executed

Be careful of timezones! Your server might be in a different timezone than your content.

// setup the start date (in a nice legible form) using any
// of the formats found on the following page:
// http://www.php.net/manual/en/datetime.formats.date.php
$contestStart = "February 1, 2012"; 

// check if current time is before the specified date
if (time() < strftime($contestStart)){
  // Display splash screen
}

// date has already passed
else {
  // Display page
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM