Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 777957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:41:25+00:00 2026-05-14T19:41:25+00:00

I’m trying to piece together a php script to output different text depending on

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:41:25+00:00Added an answer on May 14, 2026 at 7:41 pm

    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';
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 428k
  • Answers 428k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use editingAccessoryType property of UITableViewCell. May 15, 2026 at 1:25 pm
  • Editorial Team
    Editorial Team added an answer I do not know what the problem is with GetFormImage,… May 15, 2026 at 1:25 pm
  • Editorial Team
    Editorial Team added an answer Personally I would be wary of over-thinking these things too… May 15, 2026 at 1:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.