简体   繁体   English

如何从日期中减去3天?

[英]How can I subtract 3 days from a date?

I have a sync script that syncs domain expiration dates with next due date for billing. 我有一个同步脚本,用于将域到期日期与下一个到期日期进行结算。 Right now the script works great if you want the expiration date to equal the next due date. 现在,如果您希望到期日期等于下一个到期日,则脚本可以很好地工作。

But I need the next due date to be three days before the expiration, so I need the code to subtract 3 days from the $expirydate in the following code snippet (Full script code is below): 但是我需要下一个到期日是到期前三天,因此我需要以下代码片段中的$expirydate减去三天的代码(完整脚本代码如下):

if ($SyncNextDueDate) {
update_query ( "tbldomains", array ("nextduedate" => $expirydate ), array ("domain" => $domainname ) );
}

Full code: 完整代码:

<?php

require dirname ( __FILE__ ) . '/../../../dbconnect.php';
require ROOTDIR . '/includes/functions.php';
require ROOTDIR . '/includes/registrarfunctions.php';

$cronreport = 'Internet.bs Domain Sync Report<br>
---------------------------------------------------<br>
';
/**
* gets expiration date from domain list command
* @param string $data - command TEXT response
* @return array - associative array having as key the domain name and as value the expiration date
*/
function parseResult($data) {
$result = array ();
$data=strtolower($data);
$arr = explode ( "\n", $data );
$totalDomains = 0;
$assocArr = array ();
foreach ( $arr as $str ) {
    list ( $varName, $value ) = explode ( "=", $str );
    $varName = trim ( $varName );
    $value = trim ( $value );
    if ($varName == "domaincount") {
        $totalDomains = intval ( $value );
    }
    $assocArr [$varName] = $value;

}
if ($assocArr ["status"] != "success") { 
    return false;   
}

for($i = 0; $i < $totalDomains; $i ++) {
    list ( $y, $m, $d ) = explode ( "/", $assocArr ["domain_" . $i . "_expiration"] );
    $status = strtolower ( $assocArr ["domain_" . $i . "_status"] );
            if(!is_numeric($y) || !is_numeric($m) || !is_numeric($d)){
                $ddat = array ("expiry" => null, "status" => $status );
            } else {
                $ddat = array ("expiry" => mktime ( 0, 0, 0, $m, $d, $y ), "status" => $status );
            }
    $result [strtolower ( $assocArr ["domain_" . $i . "_name"] )] = $ddat;
    if (isset ( $assocArr ["domain_" . $i . "_punycode"] )) {
        $result [strtolower ( $assocArr ["domain_" . $i . "_punycode"] )] = $ddat;
    }
}
return $result;
}

$params = getregistrarconfigoptions ( 'internetbs' );

$postfields = array ();
$postfields ['ApiKey'] = $params ['Username'];
$postfields ['Password'] = $params ['Password'];
$postfields ['ResponseFormat'] = 'TEXT';
$postfields ['CompactList'] = 'no';
$testMode = trim(strtolower($params ['TestMode']))==="on";
$SyncNextDueDate = trim(strtolower($params ["SyncNextDueDate"]))==="on";

if ($testMode) {
$url = 'https://testapi.internet.bs/domain/list';
} else {
$url = 'https://api.internet.bs/domain/list';
}

$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );

curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0 );

curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_USERAGENT, "WHMCS Internet.bs Corp. Expiry Sync Robot" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postfields );

$data = curl_exec ( $ch );
$curl_err = false;


  if (curl_error ( $ch )) {
    $curl_err = 'CURL Error: ' . curl_errno ( $ch ) . ' - ' . curl_error ( $ch );
    exit ( 'CURL Error: ' . curl_errno ( $ch ) . ' - ' . curl_error ( $ch ) );
    }
    curl_close ( $ch );
    if ($curl_err) {
        $cronreport .= "Error connecting to API: $curl_err";
    } else {

    $result = parseResult ( $data );
    if (! $result) {
        $cronreport .= "Error connecting to API:<br>" . nl2br ( $data ) . "<br>";
    } else {
        $queryresult = select_query ( "tbldomains", "domain", "registrar='internetbs' AND (status='Pending Transfer' OR status='Active')" );
        while ( $data = mysql_fetch_array ( $queryresult ) ) {
            $domainname = trim ( strtolower ( $data ['domain'] ) );
            if (isset ( $result [$domainname] )) {
                                if(!is_null($result [$domainname] ["expiry"])){
                                    $expirydate = date ( "Y-m-d", $result [$domainname] ["expiry"] );
                                } else {
                                    $expirydate = false;
                                }
                $status = $result [$domainname] ["status"];
                if ($status == 'ok') {
                    update_query ( "tbldomains", array ("status" => "Active" ), array ("domain" => $domainname ) );
                }
                if ($expirydate) {
                    update_query ( "tbldomains", array ("expirydate" => $expirydate ), array ("domain" => $domainname ) );
                    if ($SyncNextDueDate) {
                        update_query ( "tbldomains", array ("nextduedate" => $expirydate ), array ("domain" => $domainname ) );
                    }
                    $cronreport .= '' . 'Updated ' . $domainname . ' expiry to ' . frommysqldate ( $expirydate ) . '<br>';
                }
            } else {
                $cronreport .= '' . 'ERROR: ' . $domainname . ' -  Domain does not appear in the account at Internet.bs.<br>';
            }
        }
    }
}
logactivity ( 'Internet.bs Domain Sync Run' );
sendadminnotification ( 'system', 'WHMCS Internet.bs Domain Syncronisation Report', $cronreport );

?>
$date = new DateTime($expirydate);
$date->sub(new DateInterval('P3D');
$expirydate = $date->format('Y-m-d');

or as a one-liner: 或单线:

$expirydate = (new DateTime($expirydate))->sub(new DateInterval('P3D'))->format('Y-m-d');

Here's a slightly different method: 这是一个稍微不同的方法:

$date = new DateTime($expirydate);
$date->modify('-3 days');
$expirydate = $date->format('Y-m-d');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM