xyn Posted September 28, 2006 Share Posted September 28, 2006 Hey,I've got a small problem with my ban script. The problem isWhen i retrive the expiry date from the table i try to checkthe time now to the time in the database, using the IF i triedto check if today is older or equal to that time, but I thinkI've used an incorrect method. Any know what i'm doing wrong?my code...[code=php:0]include "../db/db.php"; $result = mysql_query("SELECT pri_active,adm_ban FROM accounts WHERE usr_user='".strtolower($_POST['user'])."'"); while( $dat = @mysql_fetch_array($result, MYSQL_NUM)) { $ban = $dat[1]; $active = $dat[0]; if($ban=="y") { $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'"); while( $data = mysql_fetch_row( $SQL )) { $today = date("j, m, Y - H:i.s"); if( $today >= $data[4] ) { mysql_query("UPDATE accounts SET adm_ban='n' WHERE usr_user='".strtolower( $_POST['user'] )."'"); mysql_query("DLETE FROM ban WHERE user='".strtolower( $_POST['user'] )."'"); } else { die('still banned'); } } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/ Share on other sites More sharing options...
thedarkwinter Posted September 28, 2006 Share Posted September 28, 2006 Hii think the problem is that you are comparing the dates in string format. rather compare using timestamps as they are integersyou can try something like: if($ban=="y"){ $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'"); while( $data = mysql_fetch_row( $SQL )) { // get current timestamp $today = time(); // create an array from your date format $datearr = explode(",",str_replace(array(" ","-",":","."), array("",",",",",","), $data[4])); // create a timestamp from the array $bandate = mktime($datearr[3],$datearr[4],$datearr[5],$datearr[1],$datearr[0],$datearr[2]); //compare the timestamps if( $today >= $bandate ) {i believe in php5 there is a function to convert the date better from string to timestamp, but imstillon php4 :(hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100335 Share on other sites More sharing options...
wildteen88 Posted September 28, 2006 Share Posted September 28, 2006 use strtotime thedarkwinter its compatible for PHP3, 4 and 5 Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100361 Share on other sites More sharing options...
xyn Posted September 28, 2006 Author Share Posted September 28, 2006 How would i use that?[code=php:0]$Now = date("j, m, Y - H:i.s", strtotime("now"));if( $Now >= "$data[expire]"){//unban code}else{ if( $data[expire] == "never" ) { //perm banned } else { //still banned }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100373 Share on other sites More sharing options...
xyn Posted September 28, 2006 Author Share Posted September 28, 2006 [quote author=thedarkwinter link=topic=109831.msg443146#msg443146 date=1159458115]Hii think the problem is that you are comparing the dates in string format. rather compare using timestamps as they are integersyou can try something like: if($ban=="y"){ $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'"); while( $data = mysql_fetch_row( $SQL )) { // get current timestamp $today = time(); // create an array from your date format $datearr = explode(",",str_replace(array(" ","-",":","."), array("",",",",",","), $data[4])); // create a timestamp from the array $bandate = mktime($datearr[3],$datearr[4],$datearr[5],$datearr[1],$datearr[0],$datearr[2]); //compare the timestamps if( $today >= $bandate ) {i believe in php5 there is a function to convert the date better from string to timestamp, but imstillon php4 :(hope this helps[/quote]Now i get this error:Warning: mktime() expects parameter 4 to be long, string given in /home/eumod/public_html/en/global/login.php on line 128 Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100387 Share on other sites More sharing options...
alpine Posted September 28, 2006 Share Posted September 28, 2006 Do you use the mysql NOW() when adding banned time into table ban ?If so you can use interval like this when checking (change fieldname "bantime" to your actual fieldname containing time)This will reset bans older than 1 day. You can also change it to formats like example interval 3600 second (see mysql manual)[code]<?php$user = mysql_real_escape_string(strtolower($_POST['user']));$sql = mysql_query("SELECT * FROM ban WHERE user='$user' AND bantime < date_sub(now(), interval 1 day)");if(mysql_num_rows($sql) > "0"){$upt = mysql_query("UPDATE accounts SET adm_ban='n' WHERE usr_user='$user'");$del = mysql_query("DELETE FROM ban WHERE user='$user'");}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100392 Share on other sites More sharing options...
xyn Posted September 28, 2006 Author Share Posted September 28, 2006 i'm lost... Lol Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100399 Share on other sites More sharing options...
alpine Posted September 28, 2006 Share Posted September 28, 2006 I was asking in what time format you stamp the time when banning.I'm not saying that you should change your current ban script, but let me explain my solution:[code]<?php$user = mysql_real_escape_string(strtolower($_POST['user']));// ban user$ban_stamp = mysql_query("INSERT INTO ban (bantime,user) values (NOW(),'$user')");$ban_stamp_accounts = mysql_query("UPDATE accounts SET adm_ban='y' WHERE usr_user='$user'");// tikk takk - clock is ticking and time is passing by// lets see if $ban_stamp is made past 1 day ago$sql = mysql_query("SELECT * FROM ban WHERE user='$user' AND bantime < date_sub(now(), interval 1 day)");if(mysql_num_rows($sql) > "0"){$upt = mysql_query("UPDATE accounts SET adm_ban='n' WHERE usr_user='$user'");$del = mysql_query("DELETE FROM ban WHERE user='$user'");echo "$user is now free to go!";}else{echo "$user is still banned..";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100406 Share on other sites More sharing options...
xyn Posted September 28, 2006 Author Share Posted September 28, 2006 Oh i see, well my clock format is:"j, m, Y - H:i.s"But I don't want the ban to be 1 day, I wanted my modsto choose from:X minutes, X Hours, X Days, -> X being any value, couldbe 1 minute or2348 minutes... etc. Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100408 Share on other sites More sharing options...
thedarkwinter Posted September 29, 2006 Share Posted September 29, 2006 Hifirstly, wildteen88, thanks ill check it out.xyn: when you say your clock format is "j, m, Y - H:i.s", im assuming you mean your mySQL date format? If not then you need to rearrange the mktime<[url=http://uk2.php.net/manual/en/function.mktime.php]http://uk2.php.net/manual/en/function.mktime.php[/url]> so that the correct values are put in. You will also need to check that the str_replaces etc are removing the correct spaces hyphens etc.On a personal note. I would advise using a more consistant date format in mySQL: i.e use d instead of j so that the length of the string is always the same, and use H:i:s instead of H:i.s - It may not help right now - but one day... Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-100753 Share on other sites More sharing options...
Fehnris Posted September 29, 2006 Share Posted September 29, 2006 Would save yourself some work when testing for a banned date by using a UNIX timestamp to save the ban expiry date in the database.[quote]X minutes, X Hours, X Days, -> X being any value, couldbe 1 minute or2348 minutes... etc.[/quote]Im pressuming when the Mod enters X minutes, X hours, X days these values get passed to a script that creates a ban date for an account in the format "j, m, Y - H:i.s" and enters it into the database. When creating the ban expiry date use strtotime() to create a timestamp instead:-$BanTimeStamp = strtotime("+"."<X Days>"." days "."<X Hours>"." hours "."<X Minutes>"." minutes "."<X Seconds>"." seconds".Using Unix timestamps in your database you could simplify your ban checking script to:-[code]if($ban=="y"){ $SQL = mysql_query("SELECT * FROM ban WHERE user='".strtolower( $_POST['user'] )."'"); while( $data = mysql_fetch_row( $SQL )) { // get current timestamp $today = time(); //No more need to convert from your string date format to Unix timestamp for comparing as //Data[4] is already in UNIX timestamp format. //compare the timestamps if( $today >= $Data[4] ) {[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22373-time/#findComment-101017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.