简体   繁体   English

如何连接用户选择的今天的日期和小时以将其作为日期时间插入

[英]How to concatenate today's date and hour selected by user to insert it as a datetime

I have a form where user enter time :我有一个用户输入时间的表单:

<form method="post" action="requetes/ajouterHeureDebutJournee.php" role="form" id="formulaire" style="padding: 35px;">
    <input type="time" id="heure" name="heure">
    <center>
      <button class="btn btn-circle btn-small btn-2" id="valider" name="valider" type="submit"></button>
    </center>
  </form>

I receive the value in this PHP code.我收到此 PHP 代码中的值。 I want to concatenate today's date and the hour entered by the user.我想连接今天的日期和用户输入的小时。 Note : the parameter of the function is the hour entered by the user :注意:该函数的参数是用户输入的小时:

            function insertionHeureDebutJournee($date_commencement_journee)
            {
              if ($this->estPremiereConnexion()){
                $identifiant_camion = $this->GetID();
                $date_jour = Date('Y-m-d');
                $date_merge = new DateTime($date_jour->format('Y-m-d') . ' ' . $date_commencement_journee->format('H:i:s')); // This line throw an error
                $connexion = $this->GetConnexion();
                $stmt = $connexion->prepare("INSERT INTO activiteVALUES (0, ?, ?)");
                $stmt->bind_param('ss', $ID, $date_merge);
                if($stmt->execute() === false){
                  echo 'Problème dans l\'insertion de la date et l\'heure de début de journée';
                }else{
                  header('Location: ../index.php');
                }
              }
            }

How to do it ?怎么做 ? Thanks谢谢

Time elements (unless in Safari) pass in as h:i , so you have most of your work already done.时间元素(除非在 Safari 中)作为h:i传入,因此您已经完成了大部分工作。 You just need to append it to a date:您只需要将其附加到日期:

$date_jour = date('Y-m-d '.$date_commencement_journee.':00');

So if $date_commencement_journee is 16:00 , $date_jour becomes 2020-09-04 16:00:00 .因此,如果$date_commencement_journee16:00 ,则$date_jour变为2020-09-04 16:00:00 You can skip the DateTime line, since it's already formatted.您可以跳过 DateTime 行,因为它已经格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM