我想把我的日期转换成时间戳格式,这样我就可以比较时间戳和我存储的时间戳

gjmwrych  于 2021-06-15  发布在  Mysql
关注(0)|答案(3)|浏览(354)

这个问题在这里已经有答案了

我可以在php中混合mysql api吗(4个答案)
引用-这个错误在php中是什么意思(36个答案)
两年前关门了。
我想使用输入日期(2018-12-05)将其转换为时间戳格式(2018-12-05 00:00:00),以便将此时间戳与我的数据库时间戳进行比较。我应该怎么做??
我试图转换我的代码中描述的日期格式,但我得到这个错误。

<table class="table table-borderless table-data3">
<h3>Expense Table</h3>
<br>
<thead>
    <tr>
        <th>Title</th>
        <th>Amount</th>                           
        <th>Amount</th>                           
    </tr>
</thead>
<tbody>
    <?php

    if(isset($_POST['submit'])){
        $connect = new PDO("mysql:host=localhost;dbname=inventory_management_system", "root", "");
        //get input in date format like 2018-12-05
        $start_date=$_POST['start_date'];
        $end_date=$_POST['end_date'];
        $time = new DateTime('00:00:00');

        //convert date to timestamp format like 2018-12-05 00:00:00
        $date1 = new DateTime($start_date);
        $sdate = new DateTime($date1->format('Y-m-d') .' ' .$time->format('H:i:s'));
        $strDate = $sdate->format('Y-m-d H:i:s'); // Outputs like '2017-03-14 00:00:00'

        $date2 = new DateTime($end_date);
        $edate = new DateTime($date2->format('Y-m-d') .' ' .$time->format('H:i:s'));
        $endDate = $edate->format('Y-m-d H:i:s'); // Outputs like '2017-03-14 00:00:00''

        //get data between the two converted date $sdate and $edate
        $query='select amount,date from pl_per_day where STR_TO_DATE(date,"%Y-%m-%d %T")>=STR_TO_DATE($strDate,"%Y-%m-%d") AND STR_TO_DATE(date,"%Y-%m-%d %T")<=STR_TO_DATE($endDate,"%Y-%m-%d")';
                $query_run=mysqli_query($connect , $query);
                while($row=mysqli_fetch_array($query_run))
                    {
                        $moddate=strtotime($row['date']);
    ?>

    <tr>
        <td><?php echo $amount; ?></td>
        <td><?php echo $moddate; ?></td>

    <?php }  } ?>
    </tr>
</tbody>
owfi6suc

owfi6suc1#

在php脚本中,$sdate和$edate是datetime类的对象。您应该从如下对象中获取字符串值;-你的代码

$sdate = new DateTime($date1->format('Y-m-d') .' ' .$time->format('H:i:s'));
$sdate->format('Y-m-d H:i:s'); // Outputs like '2017-03-14 00:00:00'

固定 $sdate = new DateTime($date1->format('Y-m-d') .' ' .$time->format('H:i:s')); $strDate = $sdate->format('Y-m-d H:i:s'); // Outputs like '2017-03-14 00:00:00' 如果“format”方法由于某种原因失败,它将返回false,因此需要进行检查。

if (!$strDate) {
    $strDate = '1900-01-01 00:00:00';
}
xfb7svmp

xfb7svmp2#

您可以在查询本身中执行所有转换,如下所示。你可能需要把h:i:s部分添加到 $sdate 以及 $edate 在查询中,如果您需要时间比较以及。

<table class="table table-borderless table-data3">
<h3>Expense Table</h3>
<br>
<thead>
    <tr>
        <th>Title</th>
        <th>Amount</th>                           
        <th>Amount</th>                           
    </tr>
</thead>
<tbody>
    <?php

    if(isset($_POST['submit'])){
        //get input in date format like 2018-12-05
        $start_date=$_POST['start_date'];
        $end_date=$_POST['end_date'];

        //get data between the two converted date $sdate and $edate
        $query='select amount,date from pl_per_day where STR_TO_DATE(date,"%Y-%m-%d %h:%i:%s")>=STR_TO_DATE($sdate,"%Y-%m-%d") AND STR_TO_DATE(date,"%Y-%m-%d %h:%i:%s")<=STR_TO_DATE($edate,"%Y-%m-%d")';
        $query_run=mysqli_query($connect , $query);
        while($row=mysqli_fetch_array($query_run)){
            $moddate=strtotime($row['date']);
    ?>

    <tr>
        <td><?php echo $row['amount']; ?></td>
        <td><?php echo $row['date'] ?></td>

    <?php }  } ?>
    </tr>
</tbody>
mlnl4t2r

mlnl4t2r3#

$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];

$sdate = new DateTime($start_date . ' 00:00:00');
$edate = new DateTime($end_date   . ' 23:59:59');

$query = sprintf("select amount,date from pl_per_day where date between '%s' and '%s'",
    $sdate->format('Y-m-d H:i:s'),
    $edate->format('Y-m-d H:i:s')
);

相关问题