Page 1 of 2

Number of seconds that passed?

Posted: Sat Feb 10, 2007 2:01 am
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.

Posted: Sat Feb 10, 2007 7:15 am
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)

Posted: Sun Feb 11, 2007 2:38 pm
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) )
{

Posted: Mon Feb 12, 2007 6:48 am
by blackbeard
It should.

Posted: Mon Feb 12, 2007 7:57 am
by superdezign
Assuming that 'x' is somehow equivalent to an asterisk. :wink:

Posted: Mon Feb 12, 2007 9:13 am
by blackbeard
I don't even think about using asterisks, it's just something you should know. 8O

Posted: Mon Feb 12, 2007 9:21 am
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.

Posted: Mon Feb 12, 2007 10:13 am
by pickle
60 * 60 * 24 is dangerous because there are 2 days a year where that breaks (Daylight Saving Time). Use strtotime() instead.

Posted: Mon Feb 12, 2007 10:26 am
by Kieran Huggins
pickle is technically correct... which is the best kind of correct!

I miss Futurama :?

Posted: Mon Feb 12, 2007 10:39 am
by feyd
Kieran Huggins wrote:I miss Futurama :?
New episodes are coming. :)

Posted: Mon Feb 12, 2007 11:41 am
by Kieran Huggins
....in 2008!

Posted: Mon Feb 12, 2007 11:48 am
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?

Posted: Mon Feb 12, 2007 12:18 pm
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).

Posted: Mon Feb 12, 2007 12:40 pm
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 ;-)

Posted: Mon Feb 12, 2007 12:47 pm
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.