Log in

View Full Version : need help w/date compare



12Strings
05-27-2015, 05:26 AM
Hi, I just want to update a database
table column (datepaid-type is DATE(YYYY-MM-DD) to the current date
IF the duedate >= the current date, payrec = 'P', status = 'Y'and datepaid is ' '. I appreciate that my coding is dismal, sorry. Any help?


<?php
$hostname = "localhost";
$database = "homedb";
$username = "root";
$password = "cookie";
try
{
$dbconn = new PDO("mysql:host=$hostname;dbname=$database", $username,

$password);
$dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbconn->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$error = "Unable to connect to the database server.";
// Display error to user
exit();
}
$acctno="accto"; $payrec="payrec"; $status="status";
$duedate="duedate"; $datepaid="datepaid";
if($duedate == 'CURDATE')
try
{
$query = $dbconn->prepare("
UPDATE testbl SET $datepaid = CURDATE()
WHERE
payrec = 'P' AND status = 'Y' AND datepaid IS NULL
");
$query->execute(array(
":acctno" => $_POST['acctno'],
":payrec" => $_POST['payrec'],
":status" => $_POST['status'],
":duedate" => $_POST["duedate"],
":datepaid" => $_POST["datepaid"]
));
}
catch(PDOException $e)
{
$error = "Error retrieving user: ".$e->getMessage();
// Display error to user
exit();
}
?>

12Strings
07-23-2015, 10:23 PM
Hi, I have checked the validity of the table and in
all 3 records "recur" ='Y', "payrec" ='p', values are in "duedate",
"datepaid" (type is DATE) and "periodic". I'm looping thru the data; var_dump() displays NULL NULL NULL, It seems that my DATEDIFF is flawed.
I've spent considerable time viewing forums, manuals, code types. How about some advice?


<?php
error_reporting(E_ALL ^ E_NOTICE);
// error_reporting(0);
$servername = "localhost"; $username = "root";
$password = "cookie"; $dbname = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{ die("Connection failed: " . $conn->connect_error); }
// ==================================================
$sql = "SELECT recur, periodic, pd, payrec, duedate, datepaid,
DATEDIFF(CURDATE(),duedate) AS dayslate
FROM testbl WHERE recur = 'Y' && payrec = 'P'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc()) // ****3 records *****
{
// ***************************************************
var_dump($dayslate); // NULL NULL NULL
// ***************************************************
if ($dayslate > 0)
{
if($dayslate > 120)
{$pastdue = "PAST DUE";}

if($periodic == 1)
{ $duedate = date('Y-m-d', strtotime('+4 week')) ."\n"; }
if($periodic == 6)
{ $duedate = date('Y-m-d', strtotime('+25 week')) ."\n"; }
$pd = 'P'; $daylate = 0;
// ==================================================
$sql = "UPDATE testbl SET
pd = '$pd',
duedate = '$duedate',
$datepaid = 'NOW()',
dayslate = '$dayslate'
WHERE dayslate = 0";
if ($conn->query($sql) === TRUE)
{ echo "Record updated successfully"; }
else
{ echo "Error updating record: " . $conn->error; }

$conn->close();
}
}
}
// header( "refresh:3;url='http://localhost/invoice/autolist.php'");
?>