Tuesday 19 March 2019

PHP | Get List Of Dates Between Two Days

index.php
<form action="display_dates.php" method="post">
    <input type="date" name="from_date"><br/>
    <input type="date" name="to_date"><br/>
    <input type="submit" name="submit" value="find list of days">
</form>

display_dates.php
<?php
echo "<div>";
echo $From_date= $_POST['from_date'];
echo "</div>";
$to_date= $_POST['to_date'];
$day_count=0;
while(0==0)
{
    
    $day_count++;
    echo "<div>";
    echo $From_date= date('Y-m-d',strtotime($From_date . "+1 days"));
    echo "</div>";
    //if($day_count==10)
    if($From_date==$to_date)
    {
        break;
    }
    
}
?>


Video Description

Hello viewers, In this video I shall make a program to find days between two dates in PHP language.
SO as you can see, I have created one file which has a form.
I shall create a php file where we receive dates from input boxes.
I shall paste that file name in action attribute of form.
Let’s display the output first.
Everything is fine.
Now I shall receive the second date.
Day_counts variable is not that important at the end of program but.
I shall create a while loop and I need day-count variable just for testing.
So I shall break the loop when day-count reaches to 10.
It means the loop is working fine.
Now you can see the pattern of date, where Year comes first, then month then date.
So I shall overwrite the from_date variable and first convert that in date with pattern. Which is Y (dash) M (dash) D, then in second argument I shall convert from_date into time.
In short, first_date is in string, so I am first converting it in time and then in date.
We want next day after each loop, the type (dot) then in double quote, type +1 days.
Now we are hopping to the next days.
Now instead of days_count==10, what we want is if from_date== to_date so just change the condition.
So it means, we are jumping on each date but we have to stop when it reaches to end_date.
We are skipping the first day so just type echo in first lines.
Even when we select next month as end_date, it will work buecause of the inbuild program.
So lest test it, march has 31 days and end date will be 3rd of april.
Just make sure that from date is lesser that end_date otherwise the loop will run forever which is a bug.
Thank you for watching, Dont forget to like share and subscribe.