如何从日期中减去3天?

时间:2014-04-12 14:33:23

标签: php date

我有一个同步脚本,可以将域过期日期与下一个截止日期同步。现在,如果您希望到期日期等于下一个截止日期,则脚本可以正常运行。

但是我需要下一个截止日期到期前三天,所以我需要代码从以下代码段中的$expirydate中减去3天(完整脚本代码如下):

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

完整代码:

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

?>

1 个答案:

答案 0 :(得分:4)

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

或作为单行:

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

这是一种略有不同的方法:

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