Scott
Scott

Reputation: 3

PHP/JQuery - Change Page Content Based on Time

I'm trying to create a basic PHP & Javascript page that changes based on the time (US/Eastern Timezone).

I want to display an embedded video if it is between 6pm-8pm EST (every day), and outside of that time I want to have some basic HTML with text saying "Starting Soon".

I assume that we could do this using a PHP test on the time, but I really want people to be able to land on the page say at 5:45pm, then at 6pm it opens the video without them having to refresh.

The only concern I have, is if people land on the page at 7:45, it runs for 30minutes, so halfway through the page might hide the video on them...!

This is nowhere near valid code, but something like this:

if 6pm-8pmEST { } else { }

Is that even possible? Does anyone know how I could do this?

Thank you!

Upvotes: 0

Views: 423

Answers (1)

sm1215
sm1215

Reputation: 436

I wrote up a quick sample here, unfortunately running out of time right now to go into too much detail but this should be able to do what you're asking for

https://jsfiddle.net/sm1215/4ajsqd2q/5/

Basically, you can do something like this to check the time in javascript

start = 18, //18:00 = 6PM
  end = 20, //20:00 = 8PM
  now = new Date().getHours(); //will return a number between 0 - 23

    console.log(start, now, end);

if (start <= now && now >= end) {
    //play movie
}

See the fiddle for how to handle loading the video in. This technique doesn't seem to work well in the jsfiddle environment, but I've been able to use this on websites previously. Give it a try and let me know how it works out.

Upvotes: 2

Related Questions