Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is the code which i have tried with, but its only displaying the first day of Feb month, but instead i want all the odd days of the month, so what changes i should do to get the desired output?

What I have tried:

$startdate=strtotime("2/1/2018");
            $enddate=strtotime("N", $startdate);
            while ($startdate < $enddate) 
            {
            echo date(" D d, M, Y ", $startdate) . "<br>";
            $startdate = strtotime("+2 day", $startdate);
            } 
Posted
Updated 3-Apr-18 2:35am

1 solution

Setting the startdate to Feb 1st, setting the enddate +1 month and using the modulus function to check for odd days might do the trick
PHP
$startdate=strtotime("2/1/2018");
$enddate=strtotime("+1 months", $startdate);
while ($startdate < $enddate) 
{
    if (date("d", $startdate)%2==1)
    {
        $display = date(" D d, M, Y ", $startdate);
        var_dump($display);
    }
    $startdate = strtotime("+1 day", $startdate);
} 
This outputs to
string(19) " Thu 01, Feb, 2018 "
string(19) " Sat 03, Feb, 2018 "
string(19) " Mon 05, Feb, 2018 "
string(19) " Wed 07, Feb, 2018 "
string(19) " Fri 09, Feb, 2018 "
string(19) " Sun 11, Feb, 2018 "
string(19) " Tue 13, Feb, 2018 "
string(19) " Thu 15, Feb, 2018 "
string(19) " Sat 17, Feb, 2018 "
string(19) " Mon 19, Feb, 2018 "
string(19) " Wed 21, Feb, 2018 "
string(19) " Fri 23, Feb, 2018 "
string(19) " Sun 25, Feb, 2018 "
string(19) " Tue 27, Feb, 2018 "
 
Share this answer
 
Comments
1234Amit 3-Apr-18 8:40am    
sir can u please explain me the logic?
Christiaan van Bergen 3-Apr-18 8:48am    
The enddate is +1 month to the startdate (making it in this case 01 march). Because of this the while loop will work. The first check inside the while-loop is to see if the day-number is an odd number. We do this by calculating the modulo of 2 (the remainder of an odd number is always '1'). If it is odd, we display the date as you requested it. Last thing we do in the while-loop is increasing the $startdate with +1 day.
You can read up on modulo here: http://php.net/manual/en/internals2.opcodes.mod.php
1234Amit 3-Apr-18 8:51am    
thank you sir.
Maciej Los 3-Apr-18 8:53am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900