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
Post
by psychotomus » Sat Feb 10, 2007 2:01 am
$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 » Sat Feb 10, 2007 7:15 am
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 » Sun Feb 11, 2007 2:38 pm
ok. so this would tell if 24 hours have passed. correct?
Code: Select all
if (time() - $profitreset->reset >= (60 x 60 x 24) )
{
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Mon Feb 12, 2007 7:57 am
Assuming that 'x' is somehow equivalent to an asterisk.
blackbeard
Forum Contributor
Posts: 123 Joined: Thu Aug 03, 2006 6:20 pm
Post
by blackbeard » Mon Feb 12, 2007 9:13 am
I don't even think about using asterisks, it's just something you should know.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Feb 12, 2007 9:21 am
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Mon Feb 12, 2007 10:13 am
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.
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Mon Feb 12, 2007 10:26 am
pickle is technically correct... which is the best kind of correct!
I miss Futurama
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Feb 12, 2007 10:39 am
Kieran Huggins wrote: I miss Futurama
New episodes are coming.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Feb 12, 2007 11:48 am
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?
jayshields
DevNet Resident
Posts: 1912 Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England
Post
by jayshields » Mon Feb 12, 2007 12:18 pm
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).
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Mon Feb 12, 2007 12:40 pm
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
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Mon Feb 12, 2007 12:47 pm
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.