Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a date range with php?

Tags:

date

php

web

I tried to make time with PHP. start date of 2017-01-29 to 2017-12-29. But that happened I could not print in February because the month maximum of only 28 days. How to order printed anyway but with the February date up to 28.

my script :

    date_default_timezone_set('UTC');

    // Start date
    $date = '2017-01-29';
    // End date
    $end_date = '2017-12-29';

    while (strtotime($date) <= strtotime($end_date)) {
                echo "$date\n"; 
                echo "<br>";
                $date = date ("Y-m-d", strtotime("+1 month", strtotime($date)));
    } 

Output :

enter image description here

like image 690
Siti Rahmah Avatar asked Jun 05 '17 08:06

Siti Rahmah


People also ask

How do I get all dates between start and end date?

To get all of the dates between 2 dates: Copied! function getDatesInRange(startDate, endDate) { const date = new Date(startDate. getTime()); const dates = []; while (date <= endDate) { dates.

How do I find the date between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

What does date () do in PHP?

The date function in PHP is used to format the timestamp into a human desired format. The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp.


1 Answers

use this

date ("Y-m-t", strtotime("+1 month", strtotime($date)));

instead of this to get last day of the month

date ("Y-m-d", strtotime("+1 month", strtotime($date)));
like image 173
Alessandro Minoccheri Avatar answered Oct 03 '22 01:10

Alessandro Minoccheri