sorn53 Posted September 5, 2015 Share Posted September 5, 2015 (edited) Can some one please explain why the below code no longer works, it used to work fine untill i updated php on my server. $expiredate = "06-09-2015-12:00"; $expire = preg_replace('/-|:/', null, $expiredate); $expire_hour = preg_replace('/-|:/', null, $expire[3]); $expire_time = mktime($expire_hour[0], $expire_hour[1], 0, $expire[1], $expire[0], $expire[2]); if (time() > $expire_time) { continue; } if (!empty($hours_exp)) { if ((time() + 3600 * $hours_exp) >= $expire_time) { $expires = $hours . " hours!"; $email_msg = @str_replace("{expires}", $expires, $email_msg); mysql_query("UPDATE cmum_udb set `email_send` = '1' where id = '" . $id . "'"); mail($email, $subject, $email_msg, $headers); } Edited September 5, 2015 by sorn53 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/ Share on other sites More sharing options...
ginerjm Posted September 5, 2015 Share Posted September 5, 2015 uhhh - please define 'no longer works'. Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520325 Share on other sites More sharing options...
sorn53 Posted September 5, 2015 Author Share Posted September 5, 2015 (edited) uhhh - please define 'no longer works'. What do you mean ? Oh emails are not being sent anymore i believe its something to do with the mktime part $expire = preg_replace('/-|:/', null, $expiredate); $expire_hour = preg_replace('/-|:/', null, $expire[3]); $expire_time = mktime($expire_hour[0], $expire_hour[1], 0, $expire[1], $expire[0], $expire[2]); expiry dates are grabbed via mysql db, checked & when 12 hours before current date and time it sends a email out normally Edited September 5, 2015 by sorn53 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520326 Share on other sites More sharing options...
ginerjm Posted September 5, 2015 Share Posted September 5, 2015 Add some debugging echos to check the values that you are calculating and that could help you figure out the problem. Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520329 Share on other sites More sharing options...
Barand Posted September 5, 2015 Share Posted September 5, 2015 why the weird datetime format? If that is m-d-y format then just losing the "-" between date and time will work without the preg_replace()s $expiredate = "06-09-2015 12:00"; $expiretime = strtotime($expiredate); Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520330 Share on other sites More sharing options...
sorn53 Posted September 5, 2015 Author Share Posted September 5, 2015 (edited) This is the full code Functions.php <?php function send_emails($to = "") { require ("config.php"); $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or sqlerror(); mysql_select_db($dbname, $conn); $sql = mysql_query("SELECT email_msg,emailfrom,subject,hours_exp from settings WHERE id = '1'"); $row = mysql_fetch_row($sql); $email_msg_original = $row[0]; $emailfrom = $row[1]; $subject = $row[2]; $hours_exp = $row[3]; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Alert <' . $emailfrom . '>' . "\r\n"; $headers .= 'Reply-To: Reply <' . $emailfrom . '>' . "\r\n"; $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n"; if (!empty($to)) { mail($to, $subject, $email_msg_original, $headers); } else { $sql = mysql_query("SELECT * FROM dbb where email_send = 0 AND `usergroup` = '**Tester**'"); while ($row = mysql_fetch_array($sql)) { $id = $row['id']; $displayname = trim($row['name']); $email = $row['email']; $expiredate = trim($row['expiredate']); $startdate = trim($row['startdate']); $email_msg = @str_replace("{username}", $displayname, $email_msg_original); $email_msg = @str_replace("{startdate}", $startdate, $email_msg); $email_msg = @str_replace("{expiredate}", $expiredate, $email_msg); $expire = preg_replace('/-|:/', null, $expiredate); $expire_hour = preg_replace('/-|:/', null, $expire[3]); $expire_time = mktime($expire_hour[0], $expire_hour[1], 0, $expire[1], $expire[0], $expire[2]); if (time() > $expire_time) { continue; } if (!empty($hours_exp)) { if ((time() + 3600 * $hours_exp) >= $expire_time) { $expires = $hours . " hours!"; $email_msg = @str_replace("{expires}", $expires, $email_msg); mysql_query("UPDATE dbb set `email_send` = '1' where id = '" . $id . "'"); mail($email, $subject, $email_msg, $headers); } } } } } ?> cronjob.php <?php chdir("/var/www/web/"); require ("./includes/functions.php"); send_emails(); ?> Edited September 5, 2015 by sorn53 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520332 Share on other sites More sharing options...
ginerjm Posted September 5, 2015 Share Posted September 5, 2015 I don't see any attempts to debug it as I suggested. Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520333 Share on other sites More sharing options...
sorn53 Posted September 5, 2015 Author Share Posted September 5, 2015 I don't see any attempts to debug it as I suggested. im unsure as to what you suggested thats why i posted the full code, can you hightlight the parts i should change please Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520334 Share on other sites More sharing options...
sorn53 Posted September 5, 2015 Author Share Posted September 5, 2015 $expiredate = "06-09-2015 12:00"; the expire date is taken from the database which includes the 06-09-2015 - 12:00 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520335 Share on other sites More sharing options...
ginerjm Posted September 5, 2015 Share Posted September 5, 2015 (edited) I suggested adding some echo statements to show you what the values you are using really are. Add a limit=1 to your query and work through the problem looking at what your code is producing and what it has to work with. That's how one debugs a problem. Edited September 5, 2015 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520336 Share on other sites More sharing options...
sorn53 Posted September 5, 2015 Author Share Posted September 5, 2015 I suggested adding some echo statements to show you what the values you are using really are. Add a limit=1 to your query and work through the problem looking at what your code is producing and what it has to work with. That's how one debus a problem. I tried adding echo to each part but nothing was showing im very new to php sorry can you take a look please Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520337 Share on other sites More sharing options...
Barand Posted September 6, 2015 Share Posted September 6, 2015 (edited) You should always store datetimes in a DB as type DATETIME, format YYYY-MM-DD HH:MM:SS. Other formats are largely useless. For now, if you are stuck with that "-" you can $expiredate = "06-09-2015-12:00"; $dtobj = DateTime::createFromFormat('m-d-Y-H:i', $expiredate); $expiretime = $dtobj->getTimestamp(); Edited September 6, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520340 Share on other sites More sharing options...
ginerjm Posted September 6, 2015 Share Posted September 6, 2015 So - I'm guessing that you didn't write this code yourself since you can't even be bothered to look up how to do what I suggested, since you state that you 'echo' statements aren't showing anything. Being new to PHP (or any language) means that you have to do some homework, some reading, some learning, some attempt to comprehend what you are trying to achieve. Not just post some code that you gleaned and can't figure out and ask for someone else to teach you. Make the effort! Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520342 Share on other sites More sharing options...
sorn53 Posted September 6, 2015 Author Share Posted September 6, 2015 (edited) You should always store datetimes in a DB as type DATETIME, format YYYY-MM-DD HH:MM:SS. Other formats are largely useless. For now, if you are stuck with that "-" you can $expiredate = "06-09-2015-12:00"; $dtobj = DateTime::createFromFormat('m-d-Y-H:i', $expiredate); $expiretime = $dtobj->getTimestamp(); Thank you for your suggestion, ill give yours a try but i think i might have solved that part now using the code $expire_hour = date('H',strtotime($expiredate)); $expire = date('d-m-y H:i',strtotime($expiredate)); $expire_time = mktime($expire); Unfortunately im now getting PHP Notice: A non well formed numeric value encountered on line 630 which seems to be the line below causing the error. $expire_time = mktime($expire); So - I'm guessing that you didn't write this code yourself since you can't even be bothered to look up how to do what I suggested, since you state that you 'echo' statements aren't showing anything. Being new to PHP (or any language) means that you have to do some homework, some reading, some learning, some attempt to comprehend what you are trying to achieve. Not just post some code that you gleaned and can't figure out and ask for someone else to teach you. Make the effort! No i didnt write the code i used to employ a coder that unfortanately never really is around anymore, i know how to echo statments and variables etc but doing so within a function is quite new to me . I wasnt asking for the exact code on how to complete just some pointers. This is what i have done, the code is working now, im having a issue with another variable $expires = $hours . " hours!"; $expire_hour = date('H',strtotime($expiredate)); $expire = date('d-m-y H:i',strtotime($expiredate)); $expire_time = mktime($expire); Edited September 6, 2015 by sorn53 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520344 Share on other sites More sharing options...
ginerjm Posted September 6, 2015 Share Posted September 6, 2015 You're having another issue? With what? A variable? Is there a message with that problem? Give us a hand if you want our help. Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520345 Share on other sites More sharing options...
sorn53 Posted September 6, 2015 Author Share Posted September 6, 2015 (edited) A non well formed numeric value encountered with the 2 lines below. $expire_time = mktime($expire); Undefined variable: hours $expires = $hours . " hours!"; Edited September 6, 2015 by sorn53 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520346 Share on other sites More sharing options...
ginerjm Posted September 6, 2015 Share Posted September 6, 2015 What do you need here? The message pointed you to the line. Look at your code and see what you are missing that the php interpreter is seeing. Undefined variable? You haven't yet used the variable but are trying to get a value from it. A non-wellformed numeric value? Whatever you THINK is a number is NOT. Fix your code. Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520347 Share on other sites More sharing options...
Barand Posted September 6, 2015 Share Posted September 6, 2015 As I told you, that format doesn't work, echo date('Y-m-d H:i:s', strtotime("06-09-2015-12:00")) //---> 2015-09-06 13:00:00 which is why I suggested DateTime::createFromFormat('m-d-Y-H:i', $expiredate); Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520361 Share on other sites More sharing options...
scootstah Posted September 6, 2015 Share Posted September 6, 2015 (edited) You should always store datetimes in a DB as type DATETIME, format YYYY-MM-DD HH:MM:SS. Other formats are largely useless. This is pretty much the answer you should be going with. Anything else is just a bandaid. It is pointless to store a date/time in a format that MySQL does not understand, because you're then removing a whole ton of modifiers that MySQL can use to retrieve/sort/order your data. And, it's much more usable in PHP without making hacky workarounds. My suggestion would be to write a script that converts your weird format into a proper one, and then loop over each database result and update it to the correct MySQL datetime format. Edited September 6, 2015 by scootstah 1 Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520372 Share on other sites More sharing options...
Barand Posted September 6, 2015 Share Posted September 6, 2015 My suggestion would be to write a script that converts your weird format into a proper one, and then loop over each database result and update it to the correct MySQL datetime format. You don't even need to write a script - a single update query will do it. Add a new DATETIME column (eg 'newdate') to the table to store the new date first. UPDATE dbb SET newdate = STR_TO_DATE(expiredate, '%m-%d-%Y-%H:%i') Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520374 Share on other sites More sharing options...
sorn53 Posted September 6, 2015 Author Share Posted September 6, 2015 Thank you very much for all your help lads. I managed to get it working doing the following $expire_hour = date('H',strtotime($expiredate)); $expire = date('Y-m-d H:i:s', strtotime("$expiredate")); $expire_time = time($expire); if (time() > $expire_time) { continue; } if (!empty($hours_exp)) { if ((time() + 3600 * $hours_exp) >= $expire_time) { $hours = date('H',strtotime($expiredate)); regards Quote Link to comment https://forums.phpfreaks.com/topic/298071-good-evening-money-waiting-for-support-on-updating-a-function-please/#findComment-1520393 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.