从表格提交日期(例如:1982-05-20),并从今年开始返回星期几。
1)表格提交日期:
<form action="Date2.php" method="post" align="center">
<input name="date" size="20" placeholder="yyyy-mm-dd" type="text" required="">
<input name="submit" value="Submit" type="submit" id="submit" />
</form>
2)PHP部分:创建一个接受日期作为参数(从表单中)并返回星期几(星期一或星期二等)的函数。函数将在以下代码中使用:
<?
//How many dates do you need to put in the array????
$datearray = array("1/1/2016", "date", "date",......);
foreach ($datearray as $key => $date) {
echo(myDateFunction($date));
}
?>
答案 0 :(得分:0)
这是获取当天名称的功能。请注意,如果出现错误,它将抛出异常。
function fetchDay($date)
{
return (new DateTime($date))->format('l');
}
例如fetchDay('2016-04-02')
将返回"Saturday"
获取当年的一天的功能
function fetchDay($date)
{
$dateTime = new DateTime($date);
$currentYear = date('Y');
$month = $dateTime->format('m');
$day = $dateTime->format('d');
if(!checkdate($month, $day, $currentYear))
{
throw new Exception('Invalid Gregorian date');
}
$dateTime->setDate($currentYear, $month, $day);
return $dateTime->format('l');
}