date(“F”,strtotime(天-月-年))打印出一年中第二个月的错误值


date("F", strtotime(day-month-year)) printing out the wrong value for the second month of the year

只是出于好奇,任何人都知道的原因

echo date("F", strToTime("$day-02-$year"));

会打印出三月,最初我认为这是一个常见的错误,但

echo date("F", strToTime("$day-01-$year"));

打印出一月和

echo date("F", strToTime("$day-03-$year"));

打印出三月,所以我不确定这里到底发生了什么???

任何想法

您今天运行了这个脚本,所以$day=30;

因此,如果你试图获得2月30日,它会返回3月2日,或者闰年的3月1日。显然是因为二月的天数不够。。。

$day = 30;
$year = 2014;
echo date("r", strToTime("$day-02-$year"));

输出:

Sun, 02 Mar 2014 00:00:00 +0100

这不是一个bug,而是一个特性。

我的问题是将日期默认为"j",如果没有设置日期,我只是将默认值设为01,现在可以工作了,

我有一个日历,我用简单的按钮逐月查看,但我的条件语句中从未设置日期。我有一条条件语句,如果没有设置日期,我一更改默认值为小写j,它就会用正确的名称响应,因此,这个问题的答案是确保默认日期设置为1,如果不会有一天出现,例如当你每月进行时

<?php 
        if (isSet($_GET['day'])) {
            $day = $_GET['day'];
        } else {
            $day = date("01");
        }
        if (isSet($_GET['month'])) {
            $month = $_GET['month'];
        } else {
            $month = date("n");
        }
        if (isSet($_GET['year'])){
            $year = $_GET['year'];
        } else {
            $year = date("Y");
        }
        //calendar variable
        $currentTimeStamp = strToTime("$day-$month-$year");         
        //get current month name
        $monthName = date("F", $currentTimeStamp);          
        //get how many days are in the current month
        $numDays = date("t", $currentTimeStamp);            
        //counter for calendar cells in loop
        $counter = 0;       
    ?>

就像这样!