我在本地主机和服务器上有一个Cakephp 3.6
项目。我正在使用此日期选择器:
<?php
echo $this->Form->control('created', ['type' => 'text', 'label' => 'Start date']);
?>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js" ></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#created" ).datepicker({ "dateFormat": "dd-mm-yy"});
} );
</script>
在本地运行时,它可以保存所选日期,没有任何问题。当我选择日期并提交值时在服务器上使用在线版本时,它将保存今天的日期,而不是所选的日期。
本地主机和服务器之间的文件和数据库相同,我刚刚上传了它们。
编辑(完整代码)
add.ctp
<div class="customerServiceTypes form large-9 medium-8 columns content">
<?= $this->Form->create($customerServiceType) ?>
<fieldset>
<legend><?= __('Add service to customer') ?></legend>
<div ng-app="" ng-init='servicePrices = <?php echo json_encode($servicePrices); ?>;' >
<?php
echo $this->Form->control('customer_id', ['options' => $customers,'label' => 'Customer']);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes, 'label' => 'Service', 'ng-model'=>'service_type_id']);
echo $this->Form->control('price', ['label' => 'Price', 'ng-model'=>'servicePrices[service_type_id]']);
echo $this->Form->control('paid', ['label' => 'Paid', 'type'=>'number']);
echo $this->Form->control('created', ['type' => 'text', 'label' => 'Date from']);
echo $this->Form->control('card', ['label' => 'Card', 'type' => 'checkbox']);
?>
</fieldset>
<?= $this->Form->button(__('Save')) ?>
<?= $this->Form->end() ?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.5/angular.min.js" ></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#created" ).datepicker({ "dateFormat": "dd-mm-yy"});
} );
</script>
CustomerServiceTypesController.php
public function add($customerid = null)
{
$customerServiceType = $this->CustomerServiceTypes->newEntity();
if ($this->request->is('post')) {
$customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
if ($this->CustomerServiceTypes->save($customerServiceType)) {
//debug($this->request->getData("customer_id"),true);
//if field paid is not null then create a credit transaction
if(!is_null($this->request->getData("paid")))
{
$customerServiceType_paid = $this->CustomerServiceTypes->newEntity();
$customerServiceType_paid->customer_id = $customerid;
$customerServiceType_paid->service_type_id = 3; //HARDCODED
$customerServiceType_paid->price = $this->request->getData("paid");
$customerServiceType_paid->created = date("Y-m-d H:i:s");
$customerServiceType_paid->card = $this->request->getData("card");
if ($this->CustomerServiceTypes->save($customerServiceType_paid)) {
$this->Flash->success(__('Success'));
}
else{
$this->Flash->error(__('Fail'));
}
}
$this->Flash->success(__('Success'));
return $this->redirect(['controller' => 'customers', 'action' => 'edit', $customerid]);
}
$this->Flash->error(__('Fail'));
}
$customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200])->where(['Customers.id =' => $customerid]);
$serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', ['limit' => 200]);
$servicePrices = $this->CustomerServiceTypes
->ServiceTypes
->find()
->limit(200)
->combine('id','price');
$this->set(compact('customerServiceType', 'customers', 'serviceTypes','servicePrices'));
}
答案 0 :(得分:1)
请求开始日期的数据
$this->loadModel('User');
$user=$this->User->get($id);
$startDate = str_replace('/', '-', $this->request->data['start_date']);
$user->start_date $startDate->format('Y-m-d H:i:s');
$this->User->save($user);