我正在确定从该日期起的星期几。我添加了一个数组来打印出实际的月份,而不是月份的数字。它适用于正确的日期。但是当我查找无效日期时,checkdate函数会起作用,但是当显示无效日期时,它会将月份向上移动一次。我知道它与monthName有关,因为当我将它作为月份数字($ month)保留时,它可以正常工作。有什么想法吗?
<?Php
error_reporting(E_ALL ^ E_NOTICE);
$todo=$_POST['todo'];
if(isset($todo) and $todo=="submit")
{
$month=$_POST['month'];
$dt=$_POST['dt'];
$year=$_POST['year'];
$timestamp = mktime(0, 0, 0, "$month", "$dt", "$year");
$monthName = date("F", $timestamp);
if (checkdate($month, $dt, $year))
{
"<br>";
echo "$monthName $dt, $year was (or will be) on a " . \date("l", mktime(0, 0, 0, "$month", "$dt", "$year")) . "<br>";
}
else
{
echo $error = "The date $monthName $dt, $year is invalid";
}
}
?>
<h2>Enter a Date and I'll Tell You the Day of the Week</h2>
<form method=post name=f1 action=''><input type=hidden name='todo' value="submit">
<table border="0" cellspacing="0" >
<tr><td align=left>
Month <select name=month value=''>Select Month</option>
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>...
答案 0 :(得分:0)
如果您通过了无效日期,那么我猜mktime正在生成$ month + 1的时间戳,具体取决于您传递的无效日期。例如,2月30日将转换为3月1日。
就个人而言,我会抛弃mktime并使用DateTime。类似的东西: -
$month=2;
$dt=30;
$year=2014;
if(!checkdate($month, $dt, $year)){
echo "$dt " . \DateTime::createFromFormat('m', $month)->format('F') . " $year is not a valid date";
} else {
$date = \DateTime::createFromFormat('d m Y', "$dt $month $year");
echo $date->format('jS F Y') . " will be a " . $date->format('l');
}