如果您將日期選擇器<input>
元素包裝在<form>
中,並將操作設置爲同一頁面,則可以在提交表單後在同一頁面上檢查$_POST
變量。沒有提交,PHP將無法獲得變量。如果您試圖在您的服務器端腳本中使用日期而沒有提交/刷新頁面,則可以選擇查看AJAX。
下面是你可以在一個文件中設置它:
<!doctype html>
<html>
<head>
<title>Page Title Here</title>
<link rel='shortcut icon' href='favicon.ico'>
<!-- include stylesheet for jquery ui -->
<link rel='stylesheet' href='https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css'>
</head>
<body>
<?php
//check if the dates have been posted
//assign the dates to variables if they've been posted,
//otherwise assign them the value of false
$dateDebut = isset($_POST['from'])?$_POST['from']:false;
$dateFin = isset($_POST['to'])?$_POST['to']:false;
?>
<!-- the form which contains our datepickers -->
<form action='./' method='POST'>
<label for='from'>Du</label>
<input name='from' id='from'>
<label for='to'>Au</label>
<input name='to' id='to'>
<input type='submit' value='Submit'>
</form>
<?php
//print the dates if they were posted (if they aren't set to false)
if(false !== $dateDebut){
//start date ms timestamp
echo 'start date timestamp (ms) : '.$dateDebut."<br>";
//start date formatted
echo 'start date (date format - mm/dd/yyyy): '.date("m/d/Y", ($dateDebut/1000))."<br>";
}
if(false !== $dateFin){
//end date timestamp
echo 'end date timestamp (ms) : '.$dateFin."<br>";
//end date formatted
echo 'end date (date format - mm/dd/yyyy): '.date("m/d/Y", ($dateFin/1000))."<br>";
}
?>
<!-- include jquery and jquery-ui -->
<script src="https://code.jquery.com/jquery-1.12.3.min.js" integrity="sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw=" crossorigin="anonymous"></script>
<script>
//setup datepickers
$('[name=from]').datepicker({
defaultDate: "+1d",
changeMonth: true,
numberOfMonths: 1,
dateFormat : '@',
onClose: function(selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
}
});
$('[name=to]').datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
dateFormat : '@',
onClose: function(selectedDate) {
$("#from").datepicker("option", "maxDate", selectedDate);
var currentDate = $(".selector").datepicker("getDate");
}
});
</script>
</body>
</html>
是在''