-2

I'm building a script which if the 'expiration_date' field is within 30 days, it will run another .php file. I'm stuck on processing the date. Any help is appreciated!

<?php

require_once "config.php";

$expiry_date = $_GET['expiration_date'];

$query = "SELECT * FROM domains WHERE TO_DAYS(NOW()) - TO_DAYS(expiry_date) <= 30";
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
$row=mysql_fetch_row($result);
if ($row)
{

execute "ticket-create.php";

}
?>
  1. Check database .php file to get login info (config.php)
  2. Check all entries within the database. If the 'expiration_date' is within 30 days (i.e. 1st September, to 30th September), it will process ticket creation (ticket-create.php).
Barmar
  • 741,623
  • 53
  • 500
  • 612
Davebz
  • 3
  • 2
  • 1
    You shouldn't still be using the `mysql_XXX` functions. They've been deprecated for years, and removed completely in PHP 7.x. Convert to mysqli or PDO. – Barmar Sep 28 '19 at 00:14
  • What is the `$expiry_date` parameter for? How does it relate to the `expiry_date` column in the table? – Barmar Sep 28 '19 at 00:16
  • Thanks @Barmar, I've just noticed this is outdated code - I'm using mysqli on my other code! New to PHP :) Thanks! – Davebz Sep 28 '19 at 00:18
  • What do you mean by "execute ticket-create.php`? Is that another web page? Or is it a CLI script? Do you need to execute it for each row returned by the query? If so, how do the columns selected get passed to the script? – Barmar Sep 28 '19 at 00:18
  • Today is Sep 27. `TO_DAYS(NOW()) - TO_DAYS(expiry_date) <= 30` will match Aug 28 to Sep 27, is that what you want? – Barmar Sep 28 '19 at 00:21
  • It will also match all expiry dates in the future, since the result of that will be negative, which is less than 30. – Barmar Sep 28 '19 at 00:23
  • ticket-create.php is just another script that is connects to our helpdesk system via an API, which will create a ticket automatically. I'm just looking for a script to check the expiry date in a database (expiration_date) then if it is 30 days before, it will run the ticket-create.php script – Davebz Sep 28 '19 at 00:23
  • you don't usually run one PHP script from another PHP script. Usually you use `include('ticket-create.php');` to load the functions defined there, then call the functions. – Barmar Sep 28 '19 at 00:25
  • You've specified a bunch of requirements, and shown your script. What is your actual question? – Barmar Sep 28 '19 at 00:26
  • Maybe you want `ABS(DATEDIFF(NOW(), expiry_date)) <= 30` so it does 30 days in each direction, not just one direction. – Barmar Sep 28 '19 at 00:28
  • Echo $expiry_date – Strawberry Sep 28 '19 at 08:29
  • Possible duplicate of [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Sep 28 '19 at 08:39

1 Answers1

0

I did not fully understand your question, but I tried to make a proper script that best matches your question. Simply foreach SQL result it will calculate the difference between the time stored in your DB and the current time. If the time in your DB is less than 30 days it will execute the code. I can't use less than or equal to 30 as some months have 30 days and it will return as a 1 month format instead of a 30 days. Hope this helps...

<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
}
catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

// Sanitize $_GET
$GET = filter_var_array($_GET, FILTER_SANITIZE_STRING);
$expiry_date = htmlspecialchars($GET['expiration_date'], ENT_QUOTES, 'UTF-8');

$stmt = $conn->prepare("SELECT * FROM domains");
$stmt->execute();
foreach($stmt as $row){
    // Enter your DB row name corresponding with the expiry date
    $dbexpiry_date = $row[''];
    // Enter your timezone
    date_default_timezone_set('Country/City');
    $current_time = date('Y-m-d H:i:s');

    // Calculating the difference
    $dbTime = new DateTime($dbexpiry_date);
    $currTime = new DateTime($current_time);
    $interval = $currTime->diff($dbTime);
    $days = $interval->format('%d');
    $months = $interval->format('%m');
    $years = $interval->format('%Y');

    if($years == 0) {
        if($months == 0) {
            if($days < 30) {
                // Enter your code here
                execute "ticket-create.php";
            } else {
                die();
            }
        } else {
            die();
        }
    } else {
        die();
    }
}
?>
  • 1
    If you use `$days = $interval->format('%a');` it will give you the *total* number of days difference (factoring in any months and years) and you will not need the checks on months and years. – Nick Sep 28 '19 at 04:24