简体   繁体   中英

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

Just out of curiosity, anyone know why

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

would print out March, originally I thought it was a common off by one error but

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

prints out January and

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

prints out march, so I am unsure of what is actually going on here???

any ideas

You run this script today, so $day=30; ?

So, if you try to get the 30th day of February, it returns you the 2nd day of March - or March 1st in leap years. Obviously because February doesn't have enough days...

$day = 30;
$year = 2014;

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

Output:

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

It's not a bug, it's a feature.

My problem was defaulting the day as a "j" i just made the default 01 if there was no day set and it works now,

I have a calendar that I was going from month to month with simple buttons and the day was never set in my conditional statement I had that if the day was not set that the default was a lowercase j as soon as I changed that it responded with the correct name, So the answer to this question is to ensure that the default day is set to 1 if there is not going to be a day present such as when you are going from month to month

<?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;       
    ?>

like so!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM