Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the dates between two specified date?

Tags:

php

If I have two dates 20-4-2010 and 22-4-2010 in two text box and I want the dates to be like this 20, 21, 22. How do I get that ?

like image 422
udaya Avatar asked Apr 29 '10 11:04

udaya


People also ask

How do you find the date between two dates?

You can check if a date is between two dates by simply using the >= and <= operators. Typescript doesn't like this approach and will complain. To make Typescript happy, use the valueOf() function in conjunction with Date() .

How do you count the number of days between two specified dates?

DATEDIF function (derived from Date Difference) also allows you to quickly get the number of days between two dates. But unlike the DAYS function, it can do more than that. You can also use the DATEDIF function to calculate the number of months or years that have elapsed in the two given dates.

How do I calculate date between two dates in Excel?

To find the number of days between these two dates, you can enter “=B2-B1” (without the quotes into cell B3). Once you hit enter, Excel will automatically calculate the number of days between the two dates entered. Note that Excel recognizes leap years.


2 Answers

I am pretty sure this has been answered a quadrillion times before, but anyway:

$start = strtotime('20-04-2010 10:00');
$end   = strtotime('22-04-2010 10:00');
for($current = $start; $current <= $end; $current += 86400) {
    echo date('d-m-Y', $current);
}

The 10:00 part is to prevent the code to skip or repeat a day due to daylight saving time.

By giving the number of days:

for($i = 0; $i <= 2; $i++) {
    echo date('d-m-Y', strtotime("20-04-2010 +$i days"));
}

With PHP5.3

$period = new DatePeriod(
    new DateTime('20-04-2010'),
    DateInterval::createFromDateString('+1 day'),
    new DateTime('23-04-2010') // or pass in just the no of days: 2
);

foreach ( $period as $dt ) {
  echo $dt->format( 'd-m-Y' );
}
like image 135
Gordon Avatar answered Sep 28 '22 12:09

Gordon


You can use mktime().

mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.

If you increment the day number you get a valid date back, even if you go past the end of the month:

<?php
$day= 25;
$dateEnd = mktime(0,0,0,5,3,2010);
do {
    $dateCur = mktime(0,0,0,4,$day,2010);
    $day++;
    print date( 'd-m-y', $dateCur) .'<br>';
} while ($dateCur < $dateEnd);

Output:

25-04-10
26-04-10
27-04-10
28-04-10
29-04-10
30-04-10
01-05-10
02-05-10
03-05-10
like image 31
Tom Haigh Avatar answered Sep 28 '22 14:09

Tom Haigh