dongnigeng1295 2018-06-22 06:26
浏览 66
已采纳

Php - 日期和时间的差异

I need to get the difference of two Date with time. I have tried to code but it doesn't work.

HTML PAGE

<div class="col-xs-3 ">
<input type="date" class="form-control" name="planeddate">
</div> 
<div class="col-xs-3 ">
<input type="date" class="form-control" name="actualdate">
</div> 
<div class="col-xs-3 ">
<input type="time" class="form-control" name="planedtime">
</div> 
<div class="col-xs-3 ">
<input type="time" class="form-control" name="actualtime">
</div> 

PHP

<?php
include "connect.php"
$con=mysqli_connect("$host", "$username", "$password","$db_name")or die("Your Connection is in error");
$planeddate=date('Y-m-d', strtotime($_POST['planeddate']));
$actualdate=date('Y-m-d', strtotime($_POST['actualdate']));
$planedtime=$_REQUEST['planedtime'];
$actualtime=$_REQUEST['actualtime'];
$diff =date_diff($actualdate,$planeddate); 
echo $diff->format("%R%a days");
$time_diff = $actualtime - $planedtime;
echo $time_diff;
?>

I am not getting both date and time differences. Please help me out. And also calculate the difference for the input type "datetime-local" if possible.

<input type="datetime-local" name="datetime">
  • 写回答

2条回答 默认 最新

  • dpyln64620 2018-06-22 07:01
    关注

    Try this :-

    $planeddate = date('Y-m-d', strtotime($_POST['planeddate']));
    $actualdate = date('Y-m-d', strtotime($_POST['actualdate']));
    
    $datetime1 = new DateTime($planeddate);
    $datetime2 = new DateTime($actualdate);
    
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%R%a days');
    
    $time1 = new DateTime('09:00:59');
    $time2 = new DateTime('09:01:00');
    
    $interval = $time1->diff($time2);
    echo $interval->format('%s second(s)');
    

    Ref: http://php.net/manual/en/datetime.diff.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭