Jump to content

Good evening - Money waiting for support on updating a function please


Recommended Posts

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 by sorn53

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 by sorn53

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);

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 by sorn53

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 by ginerjm

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

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 by Barand

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!

 

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 by sorn53

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.

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);

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 by scootstah
  • Like 1

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')

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.