我有两个日期字段,名称为from和to。我想要的是在下一个文本框(名为diff
)中计算它们的差异。我无法在PHP中获取日期值,无论我使用下面提到的日期静态值,它都会向我显示准确的结果。
这是我的代码:
<body>
From : <input type="date" name="from">
To : <input type="date" name="from">
<?php
$datetime1 = new DateTime('2016-09-3');
$datetime2 = new DateTime('2016-09-5');
$interval = $datetime1->diff($datetime2);
?>
Diff : <input type="Text" name="diff" id="diff" value="<?php echo $interval->format('%R%a')+1;?>">
</body>
答案 0 :(得分:0)
您需要将这些输入包装到表单元素中,因此您的HTML将如下所示:
<form action="" method="post">
From : <input type="date" name="from">
To : <input type="date" name="to">
<input type="submit" value="Calculate">
</form>
然后在PHP中执行此操作:
$datetime1 = new DateTime($_POST['from']);
$datetime2 = new DateTime($_POST['to']);
嗯,我知道我没有对空输入或无效数据提出任何安全保障,但我希望这会引导您实现目标。
答案 1 :(得分:0)
工作代码:
<form>
From: <input type="date" name="from">
To: <input type="date" name="to">
<input type="submit" name="calc">
</form>
<?php
if (isset($_GET['calc'])) {
$from = filter_input(INPUT_GET, "from");
$to = filter_input(INPUT_GET, "to");
$datetime1 = new DateTime($from);
$datetime2 = new DateTime($to);
$interval = $datetime1->diff($datetime2);
echo "Diff: " . ($interval->format('%R%a') + 1);
}
?>
答案 2 :(得分:-2)
$year = floor($seconds /31556926);
$months = floor($seconds /2629743);
$week=floor($seconds /604800);
$day = floor($seconds /86400);
$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours*3600)) / 60);
$secs = floor($seconds % 60);
通过这种方式,您可以获得第二个时间,然后根据需要进行计算
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128);
key = keygen.generateKey();