Number of seconds that passed?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Number of seconds that passed?

Post by psychotomus »

$last_post initially starts out at 00000000000 as is then placed with time() after 20 seconds have passed if they perform an action.


Code: Select all

$time=time();
$diff = $time - $last_post; 
$days = $diff/86400; 
$hours = $days/24;
$minutes = $hours/60;
$seconds = $minutes/60;


echo $seconds;
im trying to sell if 20 seconds have passed. what i have will take it a few hours before it turns into seconds.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

All you need to do is this:

Code: Select all

if ((time() - $lastpost) > 20) {  // more than 20 seconds have passed

//  insert code here

}
else {

// insert other code here

}
what you were attempting to do is figure out how many days, hours, minutes, and seconds have passed. (BTW, it won't work)
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

ok. so this would tell if 24 hours have passed. correct?

Code: Select all

if (time() - $profitreset->reset >= (60 x 60 x 24) )
{
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

It should.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Assuming that 'x' is somehow equivalent to an asterisk. :wink:
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

I don't even think about using asterisks, it's just something you should know. 8O
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

60 * 60 * 24 is always the same thing. Why wouldn't you just do that math first and put that value (86400) in the code instead of using the overhead (albeit small) of having PHP calculate that for you?

Code: Select all

<?php
$timestamp = time();
if ($timestamp - $profitreset->reset >= 86400 )
{
  //
}
?>
I'd also recommend putting the time() call into a variable once, so that way you have a consistent time value to use throughout the application.
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 »

60 * 60 * 24 is dangerous because there are 2 days a year where that breaks (Daylight Saving Time). Use strtotime() instead.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

pickle is technically correct... which is the best kind of correct!

I miss Futurama :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Kieran Huggins wrote:I miss Futurama :?
New episodes are coming. :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

....in 2008!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Image

I know I am as bad as anyone here in terms of keeping a thread on topic, but can we at least try to stay close to the topic at hand?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Everah wrote:60 * 60 * 24 is always the same thing. Why wouldn't you just do that math first and put that value (86400) in the code instead of using the overhead (albeit small) of having PHP calculate that for you?
So that when you look back at your code you can clearly see what you've done (with 86400 you would definately need a comment explaining the time duration that it is, and possibly explaining why it equals that time duration).
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

This seems pretty clear to me:

Code: Select all

if (strtotime('+1 day') - time() <= $profitreset->reset)
...except for all those funny looking ascii arrows pointing every which way ;-)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Everah wrote:60 * 60 * 24 is always the same thing.
I always use 60 * 60 * 24 * X because to me it's more clear what it's doing. This is how I do it (if daylight savings are not an issue)

Code: Select all

$one_day = 60*60*24;
$fourteen_days = $one_day * 14;
one_day * 14 makes it very clear what I'm doing whereas 1209600 tells me nothing.
Post Reply