假设我有一个文件foo_version1.php,它已经上线并发布。我需要在将来的某个特定日期和时间向其添加新内容。有没有办法在发布日期等于今天的日期后显示新内容?
这是我到目前为止所做的...
<?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());
}
}
?>
这样做的问题是,每次我需要添加新内容时,我都需要创建新的 php 文件,这并不理想。所以回到问题还有别的可以优雅地做到这一点吗?
您可以将
所有内容文件放入单个"content.php"文件中。 接下来,我不会将文件传递到对象中,因为我们现在可以假设所有内容都在内容.php中。
现在我要做的是在包含期间将名称或日期传递给内容.php文件,并在 URL 中带有参数。
在您的内容.php中,我会使用带有传递参数的 switch 语句并回显出正确的内容。
代码如下:
索引.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());
}
}
?>
内容.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"] . ".";
}
?>
为了提高效率:根据每个"contentX.php"包含的内容,您可以创建一个MySQL数据库,该数据库将存储团队名称,日期和内容。然后,您的页面可以检查日期,并从数据库中获取要发布到页面的内容。
希望我能帮忙,卢克