Jump to content


Photo

Problem with time and modular (%)


  • Please log in to reply
35 replies to this topic

#1 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 28 May 2006 - 02:59 AM

Hey everyone, I'll briefly outline the situation.

I click Start -> sends the current time (in seconds) to a database.
User executes commands -> gets time from database, gets current time
Backend subtracts start time from current time = answer in seconds
Determine how many minutes it has been since the start
If it has been more than 60 minutes, END

//Determine the time number
$str_start = strtotime($str_start); // The start date becomes a timestamp
$total_seconds = time()-$str_start;
$minus_days = $total_seconds%86400;

//Make sure we aren't over an hour. If we are, set to 60 mins.
if($minus_days/3600>1){
    $seconds_to_mins=60;
    $time=60;
} else {    
    $minus_hours = $minus_days%3600;
    $seconds_to_mins = $minus_hours/60;
    $time = intval($seconds_to_mins)+1;
};

The issue is that once the seconds reach 60 minutes, it hits a % of 0. Since this isn't >1, it still lets the commands be run. Essentially, this runs for 61 minutes instead of 60. Now, I'm not quite sure how to check and make sure it is only 60 minutes, so if I start at 5:06:52, it ends at 6:06:52.

Any help would be appreciated.

Thanks,
DaVuLf

#2 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 28 May 2006 - 03:57 AM

would something like this work?
you will have to generate a new start time to test

<?php
$current_time = mktime(); // produces time in secconds : 1148786809
//insert time in database int(11)

// for testing we set $start_time

// you would query the db

$start_time = 1148786809;
$end_time = $start_time + (60 * 60); //add one hour
if($end_time <= $current_time){
    echo "time is up";
}else{

 echo   ceil(($end_time - $current_time) / 60)." Minutes left";
   
}
     



?>

Everything you want to know is here.

#3 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 28 May 2006 - 03:59 AM

Hmm.. The issue with doing this is that I have to wait an hour to test it.. And I have to get it dead on the minute, otherwise I miss my window of opportunity.

I'll give it a shot though. Thanks.

#4 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 28 May 2006 - 04:14 AM

substitute (60 * 60) with

(60 * 5) // add 5 minutes
Everything you want to know is here.

#5 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 28 May 2006 - 01:08 PM

Okay cool. Thanks :).

#6 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 12:34 AM

[!--quoteo(post=377810:date=May 28 2006, 09:08 AM:name=DaVuLf)--][div class=\'quotetop\']QUOTE(DaVuLf @ May 28 2006, 09:08 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Okay cool. Thanks :).
[/quote]

I have another question about this. I was wondering about where current_time is taken from. Is it taken from some universal source, or is it based on the PC clock.

The reason I wonder this is because this needs to maintain that all of the times are exactly synchronized between any and all computers that use the site.

Also, sort of off-topic, is there any way to make this sort of thing available on an intranet or lan? A server or something?

#7 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 01:25 PM

Nobody knows anything about this sort of thing?

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 May 2006 - 02:16 PM

The time is the time on your server. All scripts executing on the server see the same time.

Regarding you're original question.

If you store the starttime in the database in seconds, all you have to do to determine whether the current time is more than an hour later is to subtract the starttime from the current time (in seconds) and see whether the result is greater than 3600 (number of seconds in an hour).
<?php
$db_starttime = strtotime($row['starttime']); // assume this is in your code that reads the DB
$num_seconds = time() - $db_starttime;
if($num_seconds > 3600) {
//
//  more than an hour has elapsed
//
} else {
//
//  less than an hour has elaspse
//
}
?>

Ken

#9 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 02:18 PM

Okay cool, thanks. Do you know anything about the intranet?

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 May 2006 - 02:32 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Do you know anything about the intranet?[/quote]
That's a pretty broad question. What in particular do you want to know?

Ken
(also I added some more text to my first answer)

#11 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 29 May 2006 - 03:15 PM

The time is taken from the server that hosts the script. If you are truley concerned about time limits, make the users stay in the same session for the duration.

As For the intranet... as long as the server can be accessed, it is no different than the internet...pending how paranoid the administrator is..



Everything you want to know is here.

#12 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 04:00 PM

[!--quoteo(post=378088:date=May 29 2006, 11:15 AM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 11:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The time is taken from the server that hosts the script. If you are truley concerned about time limits, make the users stay in the same session for the duration. [/quote]

But if the time is always taken from the server, then do i really need a session? I'm not having any login or anything, so I don't see the benefit of sessions.

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
As For the intranet... as long as the server can be accessed, it is no different than the internet...pending how paranoid the administrator is..
[/quote]

So can I use any computer as a network? What do you mean about the administrator? Basically, I'm making a simulation for a club I'm a VP for on my campus. I've never used php or mysql before, and I've never dealt with servers or intranets or anything else of that sort. This is all very new to me. In fact, I deal mostly with Flash and Photoshop, so I'm very much a front-end guy. Anyways, the way this will be structured is that we rent a room on campus, get 5 people in front of the computers, and they enter transaction information for the other 30 or so people. The duration of this is 60 minutes, and we need to know exactly how many minutes are left, from any computer, at all times. We have become worried that if the internet goes down for some reason within the room, then our simulation will be screwed. As such, we were looking for a way around using the internet, and the topic of an intranet (or LAN) came up. I'm not sure how this works, so any and all information would be appreciated.

Sorry for the long reply.

Thanks again,
DaVuLf

[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--]Edit: Sorry Ken, I hadn't seen your reply. I think the time thing makes a little bit more sense because of that. So when I use the command 'time()', it gets the servers time, so even if my PC clock says its 12:01PM, and the server says it is 12:03PM, then it will take the 12:03PM, correct?[!--sizec--][/span][!--/sizec--]

#13 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 29 May 2006 - 04:24 PM

Most schools that I have been to have a intranet/network. For printing and such. If you place a http server on them, and the network admin does not have port 80 blocked, the sever can be access by any computer conected to the network via ip address.
You can see if the server is accessable with these addresses..

<?php 
echo $_SERVER['SERVER_ADDR'] ." Server address<br>";
echo $SERVER['SERVER_NAME'] ." Server name";
?>


If that doesn't work, bring a router to the room you rent, and plug all the computers to it and you will have your own network.
Everything you want to know is here.

#14 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 04:25 PM

[!--quoteo(post=378104:date=May 29 2006, 12:24 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 12:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If that doesn't work, bring a router to the room you rent, and plug all the computers to it and you will have your own network.
[/quote]

Yes, that is probably what I will do. Can I run this from any computer, or do I need a specific type of computer to do so?

#15 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 29 May 2006 - 04:32 PM

Oh ya, sessions could be handy for handy for managing multiple users... How are you to know what time stamp to ask the database for if more than one user is on the site?



[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Yes, that is probably what I will do. Can I run this from any computer, or do I need a specific type of computer to do so? [/quote]

You will need a server, any pc or mac with apache/php/mysql installed, and your project in the document root will do.

If you built this at home, just bring your computer, it will be the server.
Everything you want to know is here.

#16 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 04:32 PM

Well, I have a form with a submit button that records and stores the 'start' time in a database. All of the other queries base the time off of that one, so I don't really need to modify that. I'm using locks on my tables so that I can access them without worry, and that the other queries queue up (I think so anyways...).

All other timestamps accessed aren't stored in a database, they're just used to figure out how many minutes are left.

#17 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 29 May 2006 - 04:40 PM

If you do bring your own router, you will have to set it up to know where there server is. I can help with that if needed.

Everything you want to know is here.

#18 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 04:42 PM

[!--quoteo(post=378113:date=May 29 2006, 12:40 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 12:40 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If you do bring your own router, you will have to set it up to know where there server is. I can help with that if needed.
[/quote]

Well, I'm wondering about the server. The university won't let me use any of their servers, so I was wondering if I could set up a server on my laptop or something and run the database off of that. Is that possible, or do I need a special box or something?

#19 Ferenc

Ferenc
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 29 May 2006 - 04:43 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Well, I have a form with a submit button that records and stores the 'start' time in a database. [/quote]

So basicly, the timer starts when the first user submits the form.
Everything you want to know is here.

#20 DaVuLf

DaVuLf
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 29 May 2006 - 04:45 PM

[!--quoteo(post=378117:date=May 29 2006, 12:43 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 12:43 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
So basicly, the timer starts when the first user submits the form.
[/quote]

No, I control that. I have seperate 'administration' forms and php files. I initiate the timer and then there is 60 minutes until the end of the competition.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users