1
0
Fork 0
php-coding/10.work2.php

48 lines
1.9 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>练习2</title>
</head>
<body>
<form method="post" action="">
<label for="year1">:</label>
<input type="text" name="year[]" id="year1">
<label for="month1">:</label>
<input type="text" name="month[]" id="month1">
<label for="day1">:</label>
<input type="text" name="day[]" id="day1">
<label for="hour1">:</label>
<input type="text" name="hour[]" id="hour1">
<label for="minute1">:</label>
<input type="text" name="minute[]" id="minute1">
<label for="second1">:</label>
<input type="text" name="second[]" id="second1">
<br>
<label for="year2">:</label>
<input type="text" name="year[]" id="year2">
<label for="month2">:</label>
<input type="text" name="month[]" id="month2">
<label for="day2">:</label>
<input type="text" name="day[]" id="day2">
<label for="hour2">:</label>
<input type="text" name="hour[]" id="hour2">
<label for="minute2">:</label>
<input type="text" name="minute[]" id="minute2">
<label for="second2">:</label>
<input type="text" name="second[]" id="second2">
<input type="submit" value="提交">
</form>
<?php
// check if the form has been submitted
if (isset($_POST['year']) && isset($_POST['month']) && isset($_POST['day']) && isset($_POST['hour']) && isset($_POST['minute']) && isset($_POST['second'])) {
$dateTime1 = new DateTime();
$dateTime1->setTimestamp(mktime($_POST['hour'][0], $_POST['minute'][0], $_POST['second'][0], $_POST['month'][0], $_POST['day'][0], $_POST['year'][0]));
$dateTime2 = new DateTime();
$dateTime2->setTimestamp(mktime($_POST['hour'][1], $_POST['minute'][1], $_POST['second'][1], $_POST['month'][1], $_POST['day'][1], $_POST['year'][1]));
$interval = $dateTime1->diff($dateTime2);
echo $interval->format("%R%Yyears %mmonths %ddays");
}
?>
</body>