[英]show certain part of a php file once currentDate >= $publishDate
可以说我有一个文件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”文件中。 接下来,我不会将文件传递到对象中,因为我们现在可以假定所有内容都在content.php中。
现在,我要做的是在包含过程中使用URL中的参数将名称或日期传递到content.php文件。
在您的content.php中,我将使用带有传递参数的switch语句,并回显正确的内容。
代码如下:
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"] . ".";
}
?>
更加高效:根据每个“ contentX.php”所包含的内容,您可以创建一个MySQL数据库,该数据库将存储团队名称,日期和内容。 然后您的页面可以检查日期,并从数据库中获取内容以发布到页面。
希望我能帮忙,卢克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.