Treehouse: Grow your CSS skills. Land your dream job.

Time dependant image and text

  • # April 26, 2010 at 4:04 am

    I am trying to insert an image and some text at different times of the day.

    "image1.png" + "Lorem ipsum" from 12pm to 8am
    "image2.png" + "dolor sit amet" from 8am to 1pm
    "image3.png" + "Pellentesque habitant" from 1pm to 4pm
    "image1.png" + "Lorem ipsum" from 4pm to 12pm

    Does anyone know of a way to do this?

    # April 26, 2010 at 5:02 am

    you could do something simple like this:

    < ?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”);
    case 09:
    case 10:
    case 11:
    case 12:
    case 13:
    $dets = array(“img” => “image2.png”, “txt” => “dolor sit amet”);
    case 14:
    case 15:
    case 16:
    $dets = array(“img” => “image3.png”, “txt” => “Pellentesque habitant”);
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    $dets = array(“img” => “image1.png”, “txt” => “Lorem ipsum”);

    echo “$dets[txt]“;

    basically gets the date "now" you extract the hour and put it through a switch. I created an array just coz it was easier to store the stuff.

    Edit: the hours are obviously in 24hr format…

    # April 26, 2010 at 5:15 am

    Thanks I’ll try that.

    BTW where is it taking the time from? the local client or the server?
    I would prefer it took the time from the server, and also that the timezone be UTC. That way everyone would get the same result no matter where in the world they are.

    Missed something:
    I forgot to say that I want to specify the images and text per day also. Perhaps I should clearify what I want to use this for.
    I want to show when the phones at our office are open, when there’s a chance of waiting time, and when they are closed.

    green.png "Phones are open"
    Mon-thu 8am-4pm (24H: 08:00-16:00)
    fri 8am-3.30pm (24H: 08:00-15:30)
    Waiting time
    yellow.png "Phones are open. Expect waiting time"
    Mon-fri 1pm-3pm (24H: 13:00-15:00)
    red.png "Pones are closed"
    Rest of the time

    # April 26, 2010 at 5:50 am

    time is taken from the server and it is relative to a UTC date – its "Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)" so its UTC by default :)

    # April 26, 2010 at 6:34 am

    Thank you, it works perfect! :)

    Well almost anyway… I don’t know If you saw my last edit, but I also need to specify per weekday, and per minute (08:00-15:30).
    How would you go about that?

    PS Sorry if some of the questions are dumb – I’m still a noob when it comes to php ;)

    # April 26, 2010 at 10:24 am

    yes now you are getting into a bit more time coding wise… I don’t have the time to spend on it unfortunately – I was hoping you could get inspired :D

    you might find this useful… as you pass the timestamp through the date function – you can pretty much pull everything out of it.

    # April 26, 2010 at 3:30 pm

    No problem… I’ll check out the php manual and see what I can find out.

    Thanks for your help anyway! :)

    # April 26, 2010 at 4:05 pm

    No probs – let us know how you get on – we might be able to keep pointing you in the right direction if you get stuck :)

    # May 8, 2010 at 3:52 am

    Just wanted to check back with an update on this.

    I got some help from a friend, and also the good folks at stckoverflow, and ended up with this solution.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.