问题
I am working on Yii2
. In it I am trying to insert data into 3 different stages. i.e. I am using 3 queries one by one to insert different types of data. The data insertion is perfect but date-time is not. As I want to add data continuously so I have added a for loop
. But the date-time it's inserting is always the same. See my code below
for ($i = 0; $i <= 60; $i++)
{
$dt = date('Y-m-d H:i:s');
$model401->data_date_time = date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime($dt)));
$model402->data_date_time = date('Y-m-d H:i:s', strtotime('+10 minutes', strtotime($dt)));
$model403->data_date_time = date('Y-m-d H:i:s', strtotime('+15 minutes', strtotime($dt)));
.
.
.
.
.
.
}
The date-time is saving in DB is same i.e.
2020-04-25 11:11:57
first record date
2020-04-25 11:16:57
second record date
2020-04-25 11:21:57
third record date
2020-04-25 11:11:57
fourth record date
2020-04-25 11:16:57
fifth record date
2020-04-25 11:21:57
sixth record date
Table View
Update 1
for($i=0; $i<=60; $i++)
{
try {
$cust_met_data = Yii::$app->db->createCommand(
/** @lang text */ "SELECT m.`meter_id` , m.`msn` ,
m.`cust_id` , m.`device_id` FROM `mdc_meter_cust_rel` m ")->queryAll();
} catch (Exception $e) {
}
$slave_id = $cust_met_data[0]['device_id'];
$address = 0;
$count = 14;
$msn = $cust_met_data[0]['msn'];
$cust_id = $cust_met_data[0]['cust_id'];
// my base URL
$api_url = 'https://localhost:44337/api/rtu/GetData/' . $slave_id . '/' . $address . '/' . $count;
$curl = curl_init($api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$json = json_decode($curl_response);
if ($json->{0} == '06') {
echo " line 1656 ";
echo " An error occurs ";
//return $this->redirect(['index']);
} else {
echo " line 1663 ";
$vol_1 = $json->{0};
$vol_2 = $json->{1};
$vol_3 = $json->{2};
$curr_1 = $json->{3};
$curr_2 = $json->{4};
$curr_3 = $json->{5};
$kwh = $json->{6};
$dt = date('Y-m-d H:i:s');
$currDt = date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime($dt)));
$volDt = date('Y-m-d H:i:s', strtotime('+10 minutes', strtotime($dt)));
$kwhDt = date('Y-m-d H:i:s', strtotime('+15 minutes', strtotime($dt)));
$typeCurr = "401";
$typeVol = "402";
$typeKwh = "403";
/***** for current *****/
echo " adding current values ";
$model401 = new MdcmetersData();
// $model401->load(Yii::$app->request->post());
$model401->data_date_time = date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime($dt)));
$model401->device_id = $slave_id;
$model401->msn = $msn;
$model401->cust_id = $cust_id;
$model401->voltage_p1 = "";
$model401->voltage_p2 = "";
$model401->voltage_p3 = "";
$model401->current_p1 = $curr_1;
$model401->current_p2 = $curr_2;
$model401->current_p3 = $curr_3;
$model401->kwh = "";
$model401->d_type = "401";
$model401->save();
/***** for voltages *****/
echo " adding voltages values ";
$model402 = new MdcmetersData();
//$model402->load(Yii::$app->request->post());
$model402->data_date_time = date('Y-m-d H:i:s', strtotime('+10 minutes', strtotime($dt)));
$model402->device_id = $slave_id;
$model402->msn = $msn;
$model402->cust_id = $cust_id;
$model402->voltage_p1 = $vol_1;
$model402->voltage_p2 = $vol_2;
$model402->voltage_p3 = $vol_3;
$model402->current_p1 = "";
$model402->current_p2 = "";
$model402->current_p3 = "";
$model402->kwh = "";
$model402->d_type = "402";
$model402->save();
/***** for kwh *****/
echo " adding kwh values ";
$model403 = new MdcmetersData();
// $model403->load(Yii::$app->request->post());
$model403->data_date_time = date('Y-m-d H:i:s', strtotime('+15 minutes', strtotime($dt)));
$model403->device_id = $slave_id;
$model403->msn = $msn;
$model403->cust_id = $cust_id;
$model403->voltage_p1 = "";
$model403->voltage_p2 = "";
$model403->voltage_p3 = "";
$model403->current_p1 = "";
$model403->current_p2 = "";
$model403->current_p3 = "";
$model403->kwh = $kwh;
$model403->d_type = "403";
$model403->save();
}
}
}
You can see that first to 3rd and fourth to sixth date-time are the same. How can I set the date-time accurately?
Any help would be highly appreciated.
来源:https://stackoverflow.com/questions/61421783/adding-minutes-to-date-time-not-working-properly