0

I have two dates

2016-06-22 , 2016-07-11

I need to print the date in numbers for example,

22,23,24,25,26,27,28,29,30,1,2,.....11

if the month is july to august it should print

22,23,24,25,26,27,28,29,30,31,1,2,.....11

according to month wise also in PHP.

Thank you.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
Crysis
  • 418
  • 2
  • 7
  • 28

4 Answers4

3

This will work for you .. Look The DatePeriod class

A date period allows iteration over a set of dates and times, recurring at regular intervals, over a given period.

<?php

$begin = new DateTime( '2016-06-22' );
$end = new DateTime( '2016-07-11' );
$end = $end->modify( '+1 day' );

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("d") . "<br>";
}
?>

LIVE EXAMPLE : CLICK HERE

Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
3

You have to iterate over date between start and end date and print it in format d.

$fromDate = new DateTime('2016-06-22');
$toDate = new DateTime('2016-07-11');

$days = array();
while($fromDate <= $toDate) {
    $days[] =  $fromDate->format('d');
    $fromDate->modify('tomorrow');
}

echo implode(',', $days);
Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
1

Try:

function createDateRangeArray($strDateFrom,$strDateTo)
{

    // inclusive array of the dates between the from and to dates.

    // could test validity of dates here but I'm already doing
    // that in the main script

    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('d',$iDateFrom)); // first entry
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400; // add 24 hours
            array_push($aryRange,date('d',$iDateFrom));
        }
    }
    return $aryRange;
}

$arr = createDateRangeArray("2016-06-22","2016-07-11");
echo implode(",",$arr);
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
0

in mysql

select date_format(my_date_column, '%d$)  from my_table

in php

$date = new DateTime('2016-06-22');
echo $date->format('d');

echo the day number

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107