Php-日期和时间差

时间:2018-06-22 06:26:32

标签: php html

我需要获取两个Date与时间的差。我已经尝试编码,但是没有用。

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;
?>

我没有同时看到日期和时间差异。请帮帮我。 并尽可能计算输入类型“ datetime-local”的差异。

<input type="datetime-local" name="datetime">

2 个答案:

答案 0 :(得分:0)

尝试一下:-

$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)');

参考:http://php.net/manual/en/datetime.diff.php

答案 1 :(得分:0)

尝试

<?php
$planed_date_time = $_POST['planeddate'].' '.$_POST['planedtime'];
$actual_date_time = $_POST['actualdate'].' '.$_POST['actuatime'];

$planed = new DateTime($planed_date_time);
$actual = new DateTime($actual_date_time);

echo $actual->diff($planed)->format("%d days, %h hours, %i minutes, %s seconds");
?>