【问题标题】:Insert text depending on time of day and day of week根据一天中的时间和星期几插入文本
【发布时间】:2011-02-12 14:26:57
【问题描述】:

我正在尝试拼凑一个 php 脚本,以根据当天和一天中的时间输出不同的文本。

示例: 在工作日(周一至周五),我想根据以下时间段(24H,服务器时间,UTC)输出文本: 00:00-08:00:“Lorem ipsum” 08:00-13:00: "dolor sit amet" 13:00-15:00:“Pellentesque 居民” 15:00-15:30:“dolor sit amet” 15:30-24:00:“Lorem ipsum” 在周末(周六至周日),我想在这段时间内输出以下文本: 00:00-24:00 "Lorem ipsum"

任何人都可以帮助使用 php 脚本来做到这一点吗?

我已经通过css-tricks forum 获得了一些帮助。他们提供了这个代码:

    <?php
$date = strtotime("now");
$hour = date("H", $date);

switch($hour) {
case 00:
case 01:
case 02:
case 03:
case 04:
case 05:
case 06:
case 07:
case 08:
             $dets = array("img" => "image1.png", "txt" => "Lorem ipsum");
             break;
case 09:
case 10:
case 11:
case 12:
case 13:
             $dets = array("img" => "image2.png", "txt" => "dolor sit amet");
             break;
case 14:
case 15:
case 16:
             $dets = array("img" => "image3.png", "txt" => "Pellentesque habitant");
             break;
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
             $dets = array("img" => "image1.png", "txt" => "Lorem ipsum");
             break;
}


echo "<img src='$dets[img]' alt='$dets[txt]' />";   
?>

但它适用于所有日子,而且只适用于整个小时。我希望能够指定每半小时和每天。

仍然是一个 php-noob,所以我希望有人可以帮助我。

【问题讨论】:

    标签: php datetime time


    【解决方案1】:

    删除 switch 语句并使用一系列 if/else 语句。切换不会给您提供粒度而不是大量冗长。

    <?php
    
    function time_str() {
    
        $dow = date('D'); // Your "now" parameter is implied
    
        if ($dow == 'Sat' || $dow == 'Sun') {
            // weekend
            return 'Lorum Ipsum';
        }
    
        // Time in HHMM format
        $hm = (int)date("Gi");
    
        if ($hm >=    0 && $hm <  800) return 'Lorem ipsum';
        if ($hm >=  800 && $hm < 1300) return 'dolor sit amet';
        if ($hm >= 1300 && $hm < 1500) return ...
        if ($hm >= 1500 && $hm < 1530) return ...
        if ($hm >= 1530 && $hm < 2359) return ...
    }
    

    我还必须指出,您的 switch 语句有一个永远不会使用的额外案例 - 24。没有第 24 小时; 23:59 后,时钟回到 00:00。

    【解决方案2】:

    那个开关很丑。

    为什么不这样:

    <?PHP
    if (date('l') == 'Saturday' || date('l') == 'Sunday')){
       echo 'Lorem ipsum';
    }else{ //it's a weekday
       if (intval(date('H')) < 8){
         echo 'Lorem ipsum';
       }elseif(/* another expression */){
         echo "something else..
       }
    }
    

    【讨论】:

      【解决方案3】:

      感谢您的所有建议。我有一个朋友(比我更擅长 php)看看他们,我们想出了这个解决方案。 有了这个,我可以为一天中的不同时间和一周中的不同日子指定文本,并拥有一个带有它自己的文本的日期列表。

      <?php
      
      date_default_timezone_set('Europe/Copenhagen');
      
      // Runs the function
      echo time_str();
      
      function time_str() {
      
              if(IsHoliday())
              {
                  return ClosedHoliday();
              }           
      
          $dow = date('D'); // Your "now" parameter is implied
      
          if ($dow == 'Sat' || $dow == 'Sun') {
              // weekend
              return Closed();
          }
      
              // Time in HHMM
              $hm = (int)date("Gi");
      
              switch(strtolower($dow)){
                      case 'mon': //MONDAY
                          if ($hm >=    0 && $hm <  800) return Closed();
                          if ($hm >=  800 && $hm < 1100) return Open();
                          if ($hm >= 1100 && $hm < 1500) return OpenDelay();
                          if ($hm >= 1500 && $hm < 1600) return Open();
                          if ($hm >= 1600 && $hm < 2359) return Closed();
                          break;
                      case 'tue': //TUESDAY
                          if ($hm >=    0 && $hm <  800) return Closed();
                          if ($hm >=  800 && $hm < 1100) return Open();
                          if ($hm >= 1100 && $hm < 1500) return OpenDelay();
                          if ($hm >= 1500 && $hm < 1600) return Open();
                          if ($hm >= 1600 && $hm < 2359) return Closed();
                          break;              
                      case 'wed': //WEDNESDAY
                          if ($hm >=    0 && $hm <  800) return Closed();
                          if ($hm >=  800 && $hm < 1100) return Open();
                          if ($hm >= 1100 && $hm < 1500) return OpenDelay();
                          if ($hm >= 1500 && $hm < 1600) return Open();
                          if ($hm >= 1600 && $hm < 2359) return Closed();
                          break;              
                      case 'thu': //THURSDAY
                          if ($hm >=    0 && $hm <  800) return Closed();
                          if ($hm >=  800 && $hm < 1100) return Open();
                          if ($hm >= 1100 && $hm < 1500) return OpenDelay();
                          if ($hm >= 1500 && $hm < 1600) return Open();
                          if ($hm >= 1600 && $hm < 2359) return Closed(); 
                          break;              
                      case 'fri': //FRIDAY
                          if ($hm >=    0 && $hm <  800) return Closed();
                          if ($hm >=  800 && $hm < 1100) return Open();
                          if ($hm >= 1100 && $hm < 1500) return OpenDelay();
                          if ($hm >= 1500 && $hm < 1600) return Open();
                          if ($hm >= 1600 && $hm < 2359) return Closed();
                          break;              
      
              }           
      }
      
      // List of holidays
      function HolidayList()
      {
          // Format: 2009/05/11 (comma seperated)
          return array("2010/05/04","2009/05/11");
      }
      
      // Function to check if today is a holiday
      function IsHoliday()
      {
        // Retrieves the list of holidays
          $holidayList = HolidayList();
        // Checks if the date is in the holidaylist   
          if(in_array(date("Y/m/d"),$holidayList))
          { 
              return true;
        }else
          {
              return false;
          }   
      }
      
      // Returns the data when open
      function Open()
      {
              return 'Open';
      }
      
      // Return the data when closed
      function Closed()
      {
              return 'Closed';
      }
      
      // Returns the data when open but with waiting time
      function OpenDelay()
      {
              return 'Open, but with delay';
      }
      
      // Returns the data when closed due to holiday
      function ClosedHoliday()
      {
              return 'Lukket pga. helligdag';
      }
      
      ?>
      

      【讨论】:

        【解决方案4】:
            $hoursandminutes = date("H:m", $date)
        
            switch ($hoursandminutes)
        
            case "08:15":
            //do something, 
            //be aware though that the string representation 08:15 might actually be 8:15 even in 24h format
        

        我相信您也可以弄清楚如何添加日常功能。阅读日期类。

        【讨论】:

          【解决方案5】:

          我正在使用它,它可以工作,但我也会根据星期几从外部 txt 文件中加载一些文本。

          <html>
          
          <head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
          
          <META HTTP-EQUIV="refresh" CONTENT="60">
          <title>LOUNAS</title>
          <style type="text/css">
          body {
              overflow:hidden;
          }
          </style>
          <script type="text/javascript"><!--
          var imlocation = "";
           function ImageArray (n) {
             this.length = n;
             for (var i =1; i <= n; i++) {
               this[i] = ' '
             }
           }
          image = new ImageArray(7);
          image[0] = 'sunday.jpg';
          image[1] = 'monday.jpg';
          image[2] = 'tuesday.jpg';
          image[3] = 'wednsday.jpg';
          image[4] = 'thursday.jpg';
          image[5] = 'friday.jpg';
          image[6] = 'saturday.jpg';
          var currentdate = new Date();
          var imagenumber = currentdate.getDay();
          document.write('<img src="' + imlocation + image[imagenumber] + '"> style="width:100%;height:100%;" border="0" /');
          //--></script></head>
          
          <body bgcolor="#000000">
          </body>
          
          </html>
          

          【讨论】:

            猜你喜欢
            • 2016-05-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-22
            • 2017-08-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多