Jump to content

Stuck comparing two datetimes an hour before an event time in a cronjob


Recommended Posts

#!/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 by Jamied_uk

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 

 

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

 

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

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

 

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.