Code Help: Removing content based on date/time

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
TKramer
Forum Newbie
Posts: 5
Joined: Wed Sep 06, 2006 10:01 am

Code Help: Removing content based on date/time

Post by TKramer »

I have searched the forum looking for what I need but cannot find a complete solution, so I apologize in advance if this has been answered elsewhere...

I cannot figure out how to do the following, any help would be appreciated. I have recently picked up PHP and struggle sometimes.

I have a form on my page that I need to remove an "option" from a "select" element based on the date and time. The form is used to make reservations, and the e-mails that are sent from the form are not monitored after 4:30 p.m. on the date of the event.

So, I am in need of code that will a) detect the date; b) detect the time on the browser-side, not server-side, and; c) display/not display the option tag based on that information.

Thank you in advance,
Kramer
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Moved to clientside.
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Post by SpecialK »

So, I am in need of code that will a) detect the date; b) detect the time on the browser-side, not server-side, and; c) display/not display the option tag based on that information.
As for detecting the date server side:

Code: Select all

$today = getdate();
For detecting it client side

Code: Select all

var today = new Date();
My suggestion is to include the PHP date in a hidden varible so that you can use JavaScript to detect it.

Code: Select all

<input type="hidden" name="serverTime" value="$today">
You can find the full options on the PHP date and JavaScript date at the following sites
http://www.phpbuilder.com/manual/function.getdate.php
http://www.comptechdoc.org/independent/ ... adate.html
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

SpecialK wrote:My suggestion is to include the PHP date in a hidden varible so that you can use JavaScript to detect it.
That might not be helpful if the client is in a different timezone.


I'm curious as to the reason why you want to check the time client side as opposed to server side? You said the email isn't checked after 4:30pm - I assume that's your time & not client time.

However, it's always before 4:30 pm somewhere in the world - so if you check the client side time, there'll be no server time that isn't after 4:30 on at least one computer in cyberspace.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
TKramer
Forum Newbie
Posts: 5
Joined: Wed Sep 06, 2006 10:01 am

Why client-side time...

Post by TKramer »

The event that they will be reserving tickets for will be 99.9% local patrons (local to the event) and in the same time zone that the event is in.

The server time is not local.

We are trying to prevent them from trying to reserve tickets just prior to the event and the administration not seeing the online reservation request.

I hope this makes sense. I just spent another chunk of time searching the web and not finding a complete answer.

- Kramer
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Post by SpecialK »

It seems that this doesn't require anything from the client time.

You will have to compare it to an event time, which I assume is stored in a DB. If a user turns off JS then the whole client side is broken and won't prevent them from making the request. You will still need a server side check to ensure of this. The javascript will do the same thing without a post/get but shouldn't be used as 100% error checking.

You know when the event is, you know the server time. You can easily set a restriction based on the time difference from your local server time to the event time (which can be set to any timezone you desire) and return a message to the user that it's too late to reserve tickets for the event.

  • get the time from the DB

    get the current time

    use math to compare the times
Post Reply