#### Archived

This topic is now archived and is closed to further replies.

# Problem with time and modular (%)

## Recommended Posts

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

[code]
//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;
};
[/code]

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

##### Share on other sites
would something like this work?
you will have to generate a new start time to test

[code]<?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";

}

?>[/code]

##### Share on other sites
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.

##### Share on other sites
substitute (60 * 60) with

(60 * 5) // add 5 minutes

##### Share on other sites
Okay cool. Thanks :).

##### Share on other sites
[!--quoteo(post=377810:date=May 28 2006, 09:08 AM:name=DaVuLf)--][div class=\'quotetop\']QUOTE(DaVuLf @ May 28 2006, 09:08 AM) [snapback]377810[/snapback][/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?

##### Share on other sites
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).
[code]<?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
//
}
?>[/code]

Ken

##### Share on other sites
Okay cool, thanks. Do you know anything about the intranet?

##### Share on other sites
[!--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

##### Share on other sites
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..

##### Share on other sites
[!--quoteo(post=378088:date=May 29 2006, 11:15 AM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 11:15 AM) [snapback]378088[/snapback][/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.

Thanks again,
DaVuLf

[!--sizeo:1--][span style=\"font-size:8pt;line-height:100%\"][!--/sizeo--][b]Edit:[/b] 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--]

##### Share on other sites
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..

[code]<?php
echo \$SERVER['SERVER_NAME'] ." Server name";
?>[/code]

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.

##### Share on other sites
[!--quoteo(post=378104:date=May 29 2006, 12:24 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 12:24 PM) [snapback]378104[/snapback][/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?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
[!--quoteo(post=378113:date=May 29 2006, 12:40 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 12:40 PM) [snapback]378113[/snapback][/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?

##### Share on other sites
[!--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.

##### Share on other sites
[!--quoteo(post=378117:date=May 29 2006, 12:43 PM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ May 29 2006, 12:43 PM) [snapback]378117[/snapback][/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.

##### Share on other sites
Yes, your laptop will do fine, you don't need a 'special box', just a computer with a http server and a database server installed.

if this is on the laptop now, just plug it in. you will have a server on the network. make the server accessable and everything will be ok!

##### Share on other sites
And I can just download an HTTP server to set up, right?

##### Share on other sites
Yes, you can download and install a server(apache), php, and mysql to any computer if you have admin rights to the computer you wish to install them to.

did you biuld, and test this locally? locally being your laptop, or other computer you have at home.

##### Share on other sites
I have hosting with GoDaddy.com, so I run everything off my server there. Unfortunately, we're worried about the outside line being severed, or if we can't get an outside line, so we can't bank on being able to access that during the competition.

##### Share on other sites

Anyway, I would suggest installing apache/php/mysql on your laptop.
Then everything will be 'portable'.

Contact me via msn/windows messenger if you need help doing this.
see my profile