Lets say I have a file foo_version1.php and its live and published. I need to add new content to it at some specific date and time in the future. Is there a way to make the new content show once the publish date is equal to today's date?
Here's what I've done so far...
<?php
//set the time zone to LA
date_default_timezone_set('America/Los_Angeles');
class Timelock{
var $fileName;
var $contentName;
var $publishDate;
function __construct($fileName, $contentName, $publishDate){
$this->contentName = $contentName;
$this->publishDate = $publishDate;
$this->fileName = $fileName;
}
function contentName(){
return $this->contentName;
}
function publishDate(){
return $this->publishDate;
}
function fileName(){
return $this->fileName;
}
}
//create objects that has certain publish date
$chargers = new Timelock("content1.php" ,"San Diego Chargers", "2013-10-18 16:41:00");
$colts = new Timelock("content2.php" ,"Indianapolis Colts", "2013-10-18 16:41:03");
$vikings = new Timelock("content3.php" ,"Minnesota Vikings", "2013-10-18 16:41:06");
$eagles = new Timelock("content4.php" ,"Philadelphia Eagles", "2013-10-18 16:41:09");
$cowboys = new Timelock("content5.php" ,"Dallas Cowboyws", "2013-10-18 16:41:12");
//add the contents with timelocks into the array
$contentArray = array();
$contentArray[] = $chargers;
$contentArray[] = $colts;
$contentArray[] = $vikings;
$contentArray[] = $eagles;
$contentArray[] = $cowboys;
//include the file if the publish date is today
foreach($contentArray as $obj){
if( strtotime($currentDate) >= strtotime($obj->publishDate())){
include ($obj->fileName());
}
}
?>
The problem with this is that I'm going to need to create new php files every time i need to add new content which is not ideal. So back to the question is there another to do this elegantly?
You can put all your content files into a single "content.php" file. Next I would not pass the file into the object as we can now assume that the all content is in the content.php.
Now what I would do is pass the name or date to the content.php file with a parameter in the URL during the include.
In your content.php, I would use a switch statement with the passed parameter and echo out the correct content.
The code would be as follows:
index.php:
<?php
//set the time zone to LA
date_default_timezone_set('America/Los_Angeles');
class Timelock{
private $contentName;
private $publishDate;
public function __construct($contentName, $publishDate){
$this->contentName = $contentName;
$this->publishDate = $publishDate;
}
public function contentName(){
return $this->contentName;
}
public function publishDate(){
return $this->publishDate;
}
public function fileName(){
return "content.php" . "?name=" . $this->contentName;
}
}
//create objects that has certain publish date
$chargers = new Timelock("San Diego Chargers", "2013-10-18 16:41:00");
$colts = new Timelock("Indianapolis Colts", "2013-10-18 16:41:03");
$vikings = new Timelock("Minnesota Vikings", "2013-10-18 16:41:06");
$eagles = new Timelock("Philadelphia Eagles", "2013-10-18 16:41:09");
$cowboys = new Timelock("Dallas Cowboyws", "2013-10-18 16:41:12");
//add the contents with timelocks into the array
$contentArray = array();
$contentArray[] = $chargers;
$contentArray[] = $colts;
$contentArray[] = $vikings;
$contentArray[] = $eagles;
$contentArray[] = $cowboys;
//include the file if the publish date is today
foreach($contentArray as $obj){
if( strtotime($currentDate) >= strtotime($obj->publishDate())){
include ($obj->fileName());
}
}
?>
content.php:
<?php
switch($_GET["name"]) {
case "San Diego Chargers":
echo "We are in San Diego Chargers content.";
break;
default:
echo "No content found for " . $_GET["name"] . ".";
}
?>
To be even more efficient: Depending on exactly what each "contentX.php" contains you can create a MySQL database which will store team name, date, and content. Then your page can check the date, and grab the contents from the database to publish to the page.
Hope I could help, Luke
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.