tags:

views:

91

answers:

4

I'm trying to piece together a php script to output different text depending on what day it is and the time of day.

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"

Can anyone help with a php script to do that?

I've already gotten some help over at the css-tricks forum. 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.

A: 
    $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.

Jonas B
+3  A: 

Drop the switch statement and use a series of if/else statements. 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; after 23:59, the clock wraps back around to 00:00.

meagar
You need to get rid of those leading zeros champ cuz them be octals.
webbiedave
@webbiedave Yikes, of course. Thanks for catching that.
meagar
lol. You're welcome.
webbiedave
A: 

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..
   }
}
timdev
+1  A: 

Hi guys

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

?>
ploughansen