• Don't do stuff untill <time>
    8 replies, posted
I want a page to do stuff based on if a time has passed I had [code] if (strtotime($rs["banstops"]) > strtotime(date("m/d/Y H:i a"))) { //stuff to do if time has passed }else{ //keep telling them they're banned } [/code] ($rs has an array from mysql) I got that code from some place, but it doesn't work very good at all. Does anyone know a better way? I thought about having the time their ban stops be date("mdYHi") and then just compare that number with the current one, but haven't tried it yet
Well, first of all don't store time as a string. Store it a Unix timestamp, the default value that time() returns. Integers are easier to compare than strings in the first place. You could then do [code] if ($rs['banstops'] >= time()) //They're not banned else //They're banned [/code]
thank you! [editline]08:53PM[/editline] How can I add 6 hours to time()?
It's basically the number of seconds since January 1st 1970 (The Unix Epoch), so you add the hours in seconds: [code] $time = time(); $time += 60 * 60 * 6; //60 seconds in 60 minutes in 6 hours [/code] Or you can always use [url=http://ca.php.net/manual/en/function.mktime.php]mktime[/url]: [code] $time = time(); $time += mktime(6); //First parameter is hours, rest is optional [/code]
:D Thank you so much! I was looking at the php manual's article about time() ([url]http://us2.php.net/manual/en/function.time.php[/url]) and I saw in the first example '7 * 24 * 60 * 60' to get next week (7 days * 24 hours each day * 60 minutes each day * 60 seconds each minute) [B]Edit: [/B]Unfortunately, my web host is bad + bad wireless connection = Dreamweaver cannot connect to host 'blah blah blah' [B]Edit: [/B]After Ctrl+S ing a ton, it finally connects :P [editline]09:30PM[/editline] Oh wow, I didn't notice until now that [php]<? date("m-d-Y H:i A",time()+mktime(6)) ?>[/php] is giving '12-04-1912 01:23 AM' or so and I know that it's NOT 1912
Ooops, my bad, apparently it adds it to the current timestamp, which is dumb, but whatever. So all you need is: [code] $time = mktime(6); [/code] Oh and in case you're wondering why you got a weird 1912 date if it adds to the timestamp, it's because the added numbers probably exceeded the limit of an integer in PHP and thus looped around.
Integers should never overflow in php. [quote=http://us2.php.net/manual/en/language.types.integer.php]Integer overflow If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead. [code] <?php $large_number = 2147483647; var_dump($large_number); // output: int(2147483647) $large_number = 2147483648; var_dump($large_number); // output: float(2147483648) // it's true also for hexadecimal specified integers between 2^31 and 2^32-1: var_dump( 0xffffffff ); // output: float(4294967295) // this doesn't go for hexadecimal specified integers above 2^32-1: var_dump( 0x100000000 ); // output: int(2147483647) $million = 1000000; $large_number = 50000 * $million; var_dump($large_number); // output: float(50000000000) ?> [/code] [/quote]
Ah, I stand corrected, thank you.
this site is old wow
Sorry, you need to Log In to post a reply to this thread.