Jump to content

Failling to read the clock...


cmaximus

Recommended Posts

I'm working with this code that is suppose to display when the stock market is open or closed based on the users computer clock, but it's not working, could anyone please tell me what I'm doing wrong...

 

Here's the code:

 

    $clock = explode(':',''.$quote[9].'');

    if ($clock >= "4") {

        if ($clock == "00pm") {

            $stat = '<b><font color="red">Closed</font></b>';

        } else {

            $stat = '<b><font color="green">Open</font></b>';

        }

    } else {

        $stat = '<b><font color="green">Open</font></b>';

    }

Link to comment
Share on other sites

That's a good question,

  I was given this code and thought that all of the clock info was in that code snippet... this part of the code is in the header:

 

    $clock = explode(':',''.$quote[9].'');

    if ($clock >= "4") {

        if ($clock == "00pm") {

            $stat = '<b><font color="red">Closed</font></b>';

        } else {

            $stat = '<b><font color="green">Open</font></b>';

        }

    } else {

        $stat = '<b><font color="green">Open</font></b>';

    }

 

and this part of the code is in the body where the display needs to be:

 

<?=$stat// show market status?>

 

 

The website that I'm trying to implement this is http://www.stockowls.com

The market opens at 9:30am and closes at 4:00pm but the display just shows the green 'Open' all of the time...

 

 

 

 

Link to comment
Share on other sites

Hey.

I do not think the users personal time will be good for this. as it will affect the script when the clock is set wrong, or when the user is in another time zone.

 

for example:

if the users clock is 5 minutes behind and they look on the website, they will see the stock market is still open, while it is not and has not been for the last 5 minutes.(same goes if u are in another timezone)

 

why not use unix timestamp and let all the date/time checking be done server sided.

u can make new timestamps on each page request(timestamp of current time), and compare them to the opening/closing timestamps of that day.

if current time if higher then the opening time AND lower then closing time, display open, else display closed.

<?php
// $open //should have a timestamp holding opening time of today
// $close //should have a timestamp holding closing time of today
$current_time = time();  // this holds the current time

if($current_time  > $open AND $current < $close){
$stat = '<b><font color="green">Open</font></b>';
}else{
$stat = '<b><font color="red">Closed</font></b>';
}
?>

there might be more efficient ways to do this, but this is the way i would do it.

quicker, probely easyer, and no need to use anything other then php, plus its precise to the second.

hope this helps u.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.