Jamied_uk Posted September 9, 2022 Share Posted September 9, 2022 (edited) #!/usr/bin/php -q <title>J~Net Calendar Reminder</title> <?php date_default_timezone_set('Europe/London'); use PHPMailer\PHPMailer\PHPMailer; require("/var/www/html/vendor/autoload.php"); $basepath="/var/www/html/apps/calendar/"; ini_set("display_errors", "1"); error_reporting(E_ALL); $rowcount=''; ?> <style> a:hover{ color: yellow; } </style> <?php function includeFileWithVariables($fileName, $variables) { extract($variables); include($fileName); } function Deadsend($SwitchUser,$SwitchText, $time_left, $SwitchEmail_to){ $to="$SwitchEmail_to"; $from="REDACTED!"; $subject='REDACTED! Calendar Reminder Email Service'; $message='<!DOCTYPE html><html><head><meta charset="UTF-8"><title>J~Net Message</title></head><body style="margin:0px;font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px;background:#333;font-size:24px;color:#CCC;"> <a href="https://REDACTED!.sytes.net/"><img src="http://REDACTED!.sytes.net/images/logoREDACTED!.png" width="36" height="30" alt="" style="border:none;float:left;"></a><p><br>Hi there, You have a Reminder Notification For a Calendar Event<p><br>'.$SwitchText.' Time Left '.$time_left.'</div><div style="padding:24px;font-size:17px;"><br /><br /> <p> <br> <a href="https://REDACTED!.sytes.net/apps/calendar">Visit J~Net Calendar</a><br /></div></body></html>'; $mail=new PHPMailer; $mail->isSMTP(); $mail->SMTPDebug=0; // 2 is default $mail->Host='REDACTED!'; $mail->Port=587; $mail->SMTPAuth=true; $mail->Username='REDACTED!'; $mail->Password='REDACTED!'; // $mail->setFrom('REDACTED!', 'REDACTED!'); $mail->addReplyTo('REDACTED!scripts@gmail.com', 'REDACTED!'); $mail->addAddress("$to", "$to"); $mail->Subject="$subject"; $mail->msgHTML("$message"); //$mail->Body='This is a plain text message body'; //$mail->addAttachment('test.txt'); if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'The email message was sent.'; } } // $username=$_SESSION['username']; $output=$displayList=''; include_once("agoTimeFormat.php"); $myAgoObject=new convertToAgo; $today=time(); $todays_date=date('Y-m-d h:m', $today);// , time()); $todaysonehouradjustment=strtotime(time()) + 60*60; $todays_year=date("Y");//-1 $todays_month=date('m',$todaysonehouradjustment);//-1 $todays_day=date('d',$todaysonehouradjustment);//-1 $todays_hour=date('h',$todaysonehouradjustment);//-1 $e=""; require("db.php"); $sql="SELECT * FROM events WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < evt_start AND email_sent_status='0' LIMIT 1"; //2022-08-25 14:00:00 $query=mysqli_query($db_conx_crn, $sql); // Return the number of rows in result set $rowcount=mysqli_num_rows($query); $displayList="";// Initialize the display variable here $switch_status='Not Active'; while($row=mysqli_fetch_array($query, MYSQLI_ASSOC)) { //while($row=mysqli_fetch_array($query)){ $SwitchID=$row["evt_id"]; $SwitchUser=$row["username"]; $SwitchText=$row["evt_text"]; $evt_start=$row["evt_start"]; $SwitchExpire_o=$row["evt_end"]; $SwitchActive=$row["email_sent_status"]; // $Timer_template=$SwitchExpire; if($SwitchActive=='0'){ $switch_status='Active';} $SwitchExpire=strtotime($SwitchExpire_o); $difference=($evt_start-$todaysonehouradjustment); $month=date('m',$difference);//-1 $year=date('Y',$difference);//-1 $days=date('d',$difference); $hours=date('h',$difference); $mins=date('i',$difference); $hours=$hours;//-12 //$month=$month;//-1 $evt_start=substr($evt_start, 0, -3); $date_time=strftime("%Y %M, %D", strtotime($evt_start)); //$date_now=date("Y-m-d H:i:s"); //$date = '2014-04-10 14:20:15'; //$time_left=time_ago($date_time); $time_left=$month." Months ". $days." Days ".$hours." Hours ".$mins." Minutes"; if($todaysonehouradjustment>$evt_start){ $time_left="Expired!";} $displayList .= '<br />'.$time_left.'<br />Text: '.$SwitchText.'<p><br> Start '.$evt_start.' Status '.$switch_status.' Email To '. $e.'<p> Time Remaining:<p>'.$time_left.'<p> <a href="index.php">Edit</a> <p>'; //include("countdown3.php?time=$SwitchExpire"); if($time_left=="Expired!" && $todays_month==$month && $todays_year==$year){ // Get email $sql="SELECT email FROM users WHERE username='$SwitchUser' AND banned='0' LIMIT 1"; $query=mysqli_query($db_conx, $sql); $numrows=mysqli_num_rows($query) or die("Error: ".mysqli_error($db_conx)); while($row=mysqli_fetch_array($query, MYSQLI_ASSOC)) { $e=$row["email"]; } Deadsend($SwitchUser, $SwitchText, $time_left, $e); $sql="UPDATE events SET email_sent_status='1' WHERE evt_id='$SwitchID'"; //Update Code! $query=mysqli_query($db_conx_crn, $sql); }} //echo $displayList; if($rowcount > 0){ //$output=$displayList; } else { $output=' <p>No Switches Exist, <a href="create.php">Create A Calendar Reminder</a> '; } ?> <link rel="stylesheet" type="text/css" href="css/calendar.css"> <center> <p><br> <div style="float:center;height:auto;width:700px;background:#EBEBEB;margin-left:25px;margin-right:5px;box-shadow: 1px 3px 4px #666;"> <p><br> <h2>J~Net Calendar Reminder</h2> </p> <?php //$todays_date=date('Y/m/d h:i', $today); //$target_date=date('Y-m-d H:i', $SwitchExpire); echo 'Todays Time & Date: '.$todays_date; #!/usr/bin/php -q <title>Calendar Reminder</title> <?php date_default_timezone_set('Europe/London'); use PHPMailer\PHPMailer\PHPMailer; require("/var/www/html/vendor/autoload.php"); $basepath="/var/www/html/apps/calendar/"; ini_set("display_errors", "1"); error_reporting(E_ALL); $rowcount=''; ?> <style> a:hover{ color: yellow; } </style> <?php function includeFileWithVariables($fileName, $variables) { extract($variables); include($fileName); } function Deadsend($SwitchUser,$SwitchText, $time_left, $SwitchEmail_to){ $to="$SwitchEmail_to"; $from="REDACTED!"; $subject='REDACTED! Calendar Reminder Email Service'; $message='<!DOCTYPE html><html><head><meta charset="UTF-8"><title>J~Net Message</title></head><body style="margin:0px;font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px;background:#333;font-size:24px;color:#CCC;"> <a href="https://REDACTED!.sytes.net/"><img src="http://REDACTED!.sytes.net/images/logoREDACTED!.png" width="36" height="30" alt="" style="border:none;float:left;"></a><p><br>Hi there, You have a Reminder Notification For a Calendar Event<p><br>'.$SwitchText.' Time Left '.$time_left.'</div><div style="padding:24px;font-size:17px;"><br /><br /> <p> <br> <a href="https://REDACTED!.sytes.net/apps/calendar">Visit J~Net Calendar</a><br /></div></body></html>'; $mail=new PHPMailer; $mail->isSMTP(); $mail->SMTPDebug=0; // 2 is default $mail->Host='REDACTED!'; $mail->Port=587; $mail->SMTPAuth=true; $mail->Username='REDACTED!'; $mail->Password='REDACTED!'; // $mail->setFrom('REDACTED!', 'REDACTED!'); $mail->addReplyTo('REDACTED!scripts@gmail.com', 'REDACTED!'); $mail->addAddress("$to", "$to"); $mail->Subject="$subject"; $mail->msgHTML("$message"); if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'The email message was sent.'; } } // $username=$_SESSION['username']; $output=$displayList=''; include_once("agoTimeFormat.php"); $myAgoObject=new convertToAgo; $today=time(); $todays_date=date('Y-m-d h:m', $today);// , time()); $todaysonehouradjustment=strtotime(time()) + 60*60; $todays_year=date("Y");//-1 $todays_month=date('m',$todaysonehouradjustment);//-1 $todays_day=date('d',$todaysonehouradjustment);//-1 $todays_hour=date('h',$todaysonehouradjustment);//-1 $e=""; require("db.php"); $sql="SELECT * FROM events WHERE DATE_SUB(NOW(), INTERVAL 1 HOUR) < evt_start AND email_sent_status='0' LIMIT 1"; //2022-08-25 14:00:00 $query=mysqli_query($db_conx_crn, $sql); // Return the number of rows in result set $rowcount=mysqli_num_rows($query); $displayList="";// Initialize the display variable here $switch_status='Not Active'; while($row=mysqli_fetch_array($query, MYSQLI_ASSOC)) { //while($row=mysqli_fetch_array($query)){ $SwitchID=$row["evt_id"]; $SwitchUser=$row["username"]; $SwitchText=$row["evt_text"]; $evt_start=$row["evt_start"]; $SwitchExpire_o=$row["evt_end"]; $SwitchActive=$row["email_sent_status"]; // $Timer_template=$SwitchExpire; if($SwitchActive=='0'){ $switch_status='Active';} $SwitchExpire=strtotime($SwitchExpire_o); $difference=($evt_start-$todaysonehouradjustment); $month=date('m',$difference);//-1 $year=date('Y',$difference);//-1 $days=date('d',$difference); $hours=date('h',$difference); $mins=date('i',$difference); $hours=$hours;//-12 //$month=$month;//-1 $evt_start=substr($evt_start, 0, -3); $date_time=strftime("%Y %M, %D", strtotime($evt_start)); //$date_now=date("Y-m-d H:i:s"); //$date = '2014-04-10 14:20:15'; //$time_left=time_ago($date_time); $time_left=$month." Months ". $days." Days ".$hours." Hours ".$mins." Minutes"; if($todaysonehouradjustment>$evt_start){ $time_left="Expired!";} $displayList .= '<br />'.$time_left.'<br />Text: '.$SwitchText.'<p><br> Start '.$evt_start.' Status '.$switch_status.' Email To '. $e.'<p> Time Remaining:<p>'.$time_left.'<p> <a href="index.php">Edit</a> <p>'; //include("countdown3.php?time=$SwitchExpire"); if($time_left=="Expired!" && $todays_month==$month && $todays_year==$year){ // Get email $sql="SELECT email FROM users WHERE username='$SwitchUser' AND banned='0' LIMIT 1"; $query=mysqli_query($db_conx, $sql); $numrows=mysqli_num_rows($query) or die("Error: ".mysqli_error($db_conx)); while($row=mysqli_fetch_array($query, MYSQLI_ASSOC)) { $e=$row["email"]; } Deadsend($SwitchUser, $SwitchText, $time_left, $e); $sql="UPDATE events SET email_sent_status='1' WHERE evt_id='$SwitchID'"; //Update Code! $query=mysqli_query($db_conx_crn, $sql); }} //echo $displayList; if($rowcount > 0){ //$output=$displayList; } else { $output=' <p>No Switches Exist, <a href="create.php">Create A Calendar Reminder</a> '; } ?> <link rel="stylesheet" type="text/css" href="css/calendar.css"> <center> <p><br> <div style="float:center;height:auto;width:700px;background:#EBEBEB;margin-left:25px;margin-right:5px;box-shadow: 1px 3px 4px #666;"> <p><br> <h2>J~Net Calendar Reminder</h2> </p> <?php //$todays_date=date('Y/m/d h:i', $today); //$target_date=date('Y-m-d H:i', $SwitchExpire); echo 'Todays Time & Date: '.$todays_date; echo "<p>"; echo "Event Time & Date $evt_start"; echo "<p>"; echo "Time Difference! $difference"; //echo 'Target Time & Date: '.$target_date; //echo $displayList; //echo $output;?> <p><br> echo "<p>"; echo "Event Time & Date $evt_start"; echo "<p>"; echo "Time Difference! $difference"; //echo 'Target Time & Date: '.$target_date; //echo $displayList; //echo $output;?> <p><br> <p><a href="../">Back</a></p> </div> As you can see i been trying this for a while and may have redundant parts and im getting error #!/usr/bin/php -q Notice: A non well formed numeric value encountered in /var/www/html/apps/calendar/cron_test.php on line 89 Can someone tell me what im doing wrong please? I want it only to find events with and before 1 hour before event start time if its the correct year and month and day and 1 hour before event hour if this makes sense but not send if longer than an hour but less than an hour to go then email is to be sent, any ideas how to do this? Edited September 9, 2022 by Jamied_uk Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/ Share on other sites More sharing options...
Barand Posted September 9, 2022 Share Posted September 9, 2022 Not sure which block of code we should be looking at, but you do seem to be making heavy weather of calculating date differences $event_date = new DateTime('2022-05-15 15:00:00'); $now = new DateTime(); echo $event_date->diff($now)->format('%m months %d days %h hours %i minutes'); // 3 months 24 days 22 hours 34 minutes  Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600295 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 Ok in response to that i tried  while($row=mysqli_fetch_array($query, MYSQLI_ASSOC)) { $now=new DateTime(); //while($row=mysqli_fetch_array($query)){ $SwitchID=$row["evt_id"]; $SwitchUser=$row["username"]; $SwitchText=$row["evt_text"]; $evt_start=$row["evt_start"]; // $event_date $SwitchExpire_o=$row["evt_end"]; $SwitchActive=$row["email_sent_status"]; // $Timer_template=$SwitchExpire; //$evt_start=substr($evt_start, 0, -3); // if($SwitchActive=='0'){ $switch_status='Active';} $SwitchExpire=strtotime($SwitchExpire_o); $difference=$evt_start->diff($now)->format('%m months %d days %h hours %i minutes'); // 3 months 24 days 22 hours 34 minutes $month=date('m',$difference); $days=date('d',$difference); // $days=$days; $hours=date('h',$difference); $mins=date('i',$difference); // $hours=$hours-12; // $month=$month-1; // $event_date=new DateTime('2022-05-15 15:00:00'); //$time_left=time_ago($date_time); $time_left=$month." Months ". $days." Days ".$hours." Hours ".$mins." Minutes"; And i cant see any output  echo 'Time & Date: '.$now; echo "<p>"; echo "Event Start $evt_start"; echo "Difference: $difference";  Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600298 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 Also 2022-09-09 14:55:00 is in a db entry! should i strip last 3 chars from it? Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600299 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 (edited) Error Fatal error: Uncaught Error: Call to a member function diff() on string in /var/www/html/apps/calendar/cron_test2.php:91 Stack trace: #0 {main} thrown in /var/www/html/apps/calendar/cron_test2.php on line 91 Â that line is $now=new DateTime(); $difference=$evt_start->diff($now)->format('%m months %d days %h hours %i minutes'); Â Edited September 9, 2022 by Jamied_uk Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600301 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 even tried a function function Diff($now, $evt_start){ $now=new DateTime(); echo new DateTime($evt_start)->diff($now)->format('%m months %d days %h hours %i minutes'); } Â Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600302 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 tried with and without echo and get blank pages now! Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600303 Share on other sites More sharing options...
Barand Posted September 9, 2022 Share Posted September 9, 2022 You need a datetime object when using diff(), you are using you start time string. Re-read my example $event_date = new DateTime($row['evt_start']); $now = new DateTime(); $time_left = $event_date->diff($now)->format('%m months %d days %h hours %i minutes'); What were you expecting with lines like these? ... ( https://www.php.net/manual/en/function.date.php ) $month=date('m',$difference); $days=date('d',$difference); Â Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600306 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 I want to make sure it has 0 days difference etc also it needs to be H and Y, but i cant get it working 100% any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600310 Share on other sites More sharing options...
dodgeitorelse3 Posted September 9, 2022 Share Posted September 9, 2022 (edited) Blank page is probably due to this line  $evt_start=$row["evt_start"]; // $event_date Edited September 9, 2022 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600314 Share on other sites More sharing options...
Barand Posted September 9, 2022 Share Posted September 9, 2022 1 hour ago, Jamied_uk said: also it needs to be H and Y That needs extra explanation Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600318 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 H is the hour in 24 hour format example 18:00 and Y is the full 4 char date! example 1999 Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600319 Share on other sites More sharing options...
Barand Posted September 9, 2022 Share Posted September 9, 2022 What needs to be H and Y? The datetimes already are. Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600320 Share on other sites More sharing options...
Jamied_uk Posted September 9, 2022 Author Share Posted September 9, 2022 thats not the issue the line issue is with the $time_left = $event_date->diff($now)->format('%m months %d days %h hours %i minutes'); style line! i exaplined that up there! ^^^ Quote Link to comment https://forums.phpfreaks.com/topic/315297-stuck-comparing-two-datetimes-an-hour-before-an-event-time-in-a-cronjob/#findComment-1600321 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.