Compare todays with date in db and check weather it is 30 days older

Viewed 50

I have varoius useres stored in databse. I need to check each users date and compare todays date with date in db, and if its today is the 30th day from date in db execute a curl. Following is my code

public function repeatPayment()
{
    //return "hi22";
    $users=Payment::all();
    foreach ($users as $user)
    {
        $trans_id = $user->transactionid;
        $clinicid = $user->clinicID;
        $last_transaction_date = $user->updated_at;
        echo $last_transaction_date."<br>";
        if (strtotime($last_transaction_date) < strtotime('-30 days')) {
            echo "hello";
        }
        $payment = array(
            "MerchantID"=> "0000000001",
            "Login"=> "API0000000001",
            "Password"=> "Temp1234!",
            "NewExpDate"=> "102020",
            "Amount"=> "29.99",
            "Token"=>$trans_id,
            "StandardEntryCode"=> "string",
            "AuthOnly"=> false
        );
        
        $post_field = json_encode($payment);
        // var_dump($post_field);
        // die();
        $ch = curl_init();
        $headers = array(
            "Content-type: application/json",
            "Cache-Control: no-cache",
        );
        curl_setopt($ch, CURLOPT_URL,"https://api.securepds.com/2.0/payments.svc/JSON/SubmitTokenPayment");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post_field);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $server_output = curl_exec($ch);
        curl_close ($ch);
        $payment_id=  json_decode($server_output,true);
        $newTransId= new CronJob;
        $newTransId->clinic_id = $clinicid;
        $newTransId->tranascation_id = $payment_id["Confirmation"];
        $newTransId->transaction_date = date('d/m/Y');
        $newTransId->save();
    }
}
3 Answers

Assuming you have same timezone and date format both in MySQL and Php configuration. Below is code snippet which will do your stuffs:

$datetime1 = new DateTime('2020-06-15');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
$days_diff = intval($interval->format('%a'));
if($days_diff > 30 ){
    //Do your stuffs
}

DateTime is better way to handle time zone and Day light savings.

You can use Carbon as follows :

if (30 - ((new \Carbon\Carbon($last_transaction_date, 'UTC'))->diffInDays()) < 0) {
    echo "The date is older than 30 days";
}

Or, If you just want to check if it's the start of the day, then it's fairly easy to check with Carbon's endOfDay() :

if( $last_transaction_date->endOfDay->diffInDays() > 30 ){
   echo "older than 30 days";
}

I would like to add an answer since I believe the answers so far is a little too much to solve a simple issue.

$user->updated_at field is a Carbon date instance, and therefore, $last_transaction_date is as well. Carbon has a function diffInDays() that gives you the difference in days between today and the given date. So in OP's case, I believe something like this would be simpler.

if( $last_transaction_date->diffInDays() > 30 )
  echo "more than 30 days"
Related