Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

php dynamically create date range for next 12 months with interval of 15 days

I want to create dropdown options dynamically with php as follows

01-March-2024 To 31-March-2024
15-March-2024 To 15-April-2024
01-April-2024 To 30-April-2024
15-April-2024 To 15-May-2024
01-May-2024 To 31-May-2024
15-May-2024 To 15-June-2024
01-June-2024 To 30-June-2024
15-June-2024 To 15-July-2024 ... And so on for next upcoming 12 months

What I tried is giving me as follows

01-March-2024 To 31-March-2024
01-April-2024 To 30-April-2024
01-May-2024 To 31-May-2024
01-June-2024 To 30-June-2024
01-July To 31 July 2024 ... And so on for next upcoming 12 months

PHP Code I am Using :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<select name="duration">
<?php
  for($i=0;$i<=12;$i++) {
    $time1 = strtotime("first day of +". $i." months");
    $time2 = strtotime("last day of +". $i." months");
?>
    <option value="<?php echo date("d-F-Y", $time1)?> To <?php echo date("d-F-Y", $time2)?>"><?php echo date("d-F-Y", $time1)?> To <?php echo date("d-F-Y", $time2)?></option>
 <?php } ?>
 </select>

>Solution :

I was a little wrong in that strtotime apparently doesn’t like fifteenth of $i months, but you can override the day in the date command:

<select name="duration">
    <?php
    for($i=0;$i<=12;$i++) {
        $time1 = strtotime("first day of +". $i." months");
        $time2 = strtotime("last day of +". $i." months");
        ?>
        <option value="<?php echo date("d-F-Y", $time1)?> To <?php echo date("15-F-Y", $time1)?>"><?php echo date("d-F-Y", $time1)?> To <?php echo date("15-F-Y", $time1)?></option>
        <option value="<?php echo date("15-F-Y", $time1)?> To <?php echo date("d-F-Y", $time2)?>"><?php echo date("15-F-Y", $time1)?> To <?php echo date("d-F-Y", $time2)?></option>
    <?php } ?>
</select>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading