mardi 17 novembre 2020

How to put multiple condition on foreach loop using php

Below is my code:

function ublox_salesforce_salesforce_webforms_save_submission_alter(&$fields, $context) {
  foreach ($fields as $key => $value) {
    if (in_array($key, ["Date_Prototype__c", "Date_Production__c", "NBIOT_Date_of_Field_Trial__c", "NBIOT_Date_Lab_Trial__c"])) {
      $fields[$key] = trim($value);

      if ($key == 'Date_Production__c') {
        $production_date = date("Y-m-d",strtotime($fields['Date_Production__c']));
        $fields[$key] = $production_date;
      }
}

You can see I have put condition on "Date_production__c" and it is working fine for me now I want to apply same condition on "Date_Prototype__c" how I can do apply?

I have tried by copy paste my condition as with 'Date_Production__C"

Like below:

          function ublox_salesforce_salesforce_webforms_save_submission_alter(&$fields, $context) {
  foreach ($fields as $key => $value) {
    if (in_array($key, ["Date_Prototype__c", "Date_Production__c", "NBIOT_Date_of_Field_Trial__c", "NBIOT_Date_Lab_Trial__c"])) {
      $fields[$key] = trim($value);

      if ($key == 'Date_Production__c') {
        $production_date = date("Y-m-d",strtotime($fields['Date_Production__c']));
        $fields[$key] = $production_date;
      }

      if ($key == 'Date_Prototype__c') {
        $prototype_date = date("Y-m-d",strtotime($fields['Date_Prototype__c']));
        $fields[$key] = $prototype_date;
      }
}

But it is not working as when i print 'Date_Prototype__c' using drupal_set_message it is printing wrong date like 1970-01-01 bit it is working fine with first condition with 'Date_Production__c'

any idea? where I am doing wrong? Your help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire