Php如果在-30天之前执行操作

问题描述 投票:0回答:1

我正在构建一个脚本,如果'expiration_date'字段在30天内,它将运行另一个.php文件。我一直在处理日期。任何帮助表示赞赏!

<?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. 检查数据库.php文件以获取登录信息(config.php)
  2. 检查数据库中的所有条目。如果“ expiration_date”在30天内(即9月1日至9月30日),它将处理票证创建(ticket-create.php)。
php mysql
1个回答
0
投票

我没有完全理解您的问题,但是我尝试制作出最适合您问题的脚本。简单地foreach SQL结果,它将计算存储在数据库中的时间与当前时间之间的差。如果数据库中的时间少于30天,它将执行代码。我不能使用小于或等于30的格式,因为某些月份有30天,它将以1个月的格式而不是30天的格式返回。希望这会有所帮助...

<?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();
    }
}
?>
© www.soinside.com 2019 - 2024. All rights reserved.