简体   繁体   English

根据一天中的时间和星期几插入文本

[英]Insert text depending on time of day and day of week

I'm trying to piece together a php script to output different text depending on what day it is and the time of day. 我正在尝试拼凑一个php脚本,以根据当天和一天中的时间输出不同的文本。

Example: On weekdays (mon-fri), I would like to output text according to the following periods of time (24H, server time, UTC): 00:00-08:00: "Lorem ipsum" 08:00-13:00: "dolor sit amet" 13:00-15:00: "Pellentesque habitant" 15:00-15:30: "dolor sit amet" 15:30-24:00: "Lorem ipsum" On weekends (sat-sun), I would like to output the following text in this time period: 00:00-24:00 "Lorem ipsum" 示例:在工作日(周一至周五),我想根据以下时间段(24小时,服务器时间,UTC)输出文本:00:00-08:00:“ Lorem ipsum” 08:00-13: 00:“阿勒特坐” 13:00-15:00:“佩伦特式居民” 15:00-15:30:“阿勒坐子” 15:30-24:00:“ Lorem ipsum”周末(周六至周日) ),我想在此时间段内输出以下文本:00:00-24:00“ Lorem ipsum”

Can anyone help with a php script to do that? 任何人都可以帮助一个PHP脚本做到这一点?

I've already gotten some help over at the css-tricks forum . 我已经在css-tricks论坛上获得了一些帮助。 They supplied this code: 他们提供了以下代码:

    <?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]' />";   
?>

But it works for all days, and only in full hours. 但是它可以全天工作,并且只能在整个小时内工作。 I want to be able to specify per half-hour and on a day to day basis. 我希望能够每天指定每半小时一次。

Still a php-noob so I'm hoping someone can help me. 仍然是php-noob,所以我希望有人可以帮助我。

Drop the switch statement and use a series of if/else statements. 删除switch语句,并使用一系列if / else语句。 The switch isn't going to give you the granularity without being massively verbose. 该开关不会给您带来粒度,而又不会太冗长。

<?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 ...
}

I also have to point out that your switch statement has an extra case that will never be used - 24. There is no 24th hour; 我还必须指出,您的switch语句有一个永远不会使用的额外情况-24。 after 23:59, the clock wraps back around to 00:00. 23:59之后,时钟回绕到00:00。

That switch is ugly. 那个开关很丑。

Why not something like: 为什么不这样:

<?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..
   }
}

Thanks for all your suggestions. 感谢您的所有建议。 I had a friend (whos a little better at php than me) look at them, and we came up with this solution. 我有一个朋友(在PHP方面比我更好一点)看着他们,我们提出了这个解决方案。 With this, I am able to specify text for different times of the day, and different days of the week, along with having a list of days with it's very own text. 这样,我就可以为一天中的不同时间,一周中的不同天指定文本,并且可以使用自己的文本来列出日期。

<?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';
}

?>
    $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

I'm sure you can figure out how to go about adding day to day functionallity as well. 我相信您也可以弄清楚如何增加日常功能。 Read up on the date class. 阅读日期类。

I am using this, its working, but I would also load some text from an external txt file depending on the day of week. 我正在使用它,它可以正常工作,但是我还会根据星期几从外部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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM