簡體   English   中英

一旦currentDate> = $ publishDate,顯示php文件的特定部分

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM