Insert text depending on time of day and day of week.

Posted by ploughansen on Stack Overflow See other posts from Stack Overflow or by ploughansen
Published on 2010-04-26T21:14:40Z Indexed on 2010/04/26 21:23 UTC
Read the original article Hit count: 209

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about datetime