How we can get specific days from month within given date range in PHP?
If you are facing problem with "to get specific days from two months". I have added below solution that are working fine for me. Hope this will useful for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $cdate = date ( 'd-m-Y' ); //this is current date $date = strtotime ( '+2 months' ); //this is date after two months. $nextDate = date ( 'd-m-Y' , $date ); $dates = dateRange( $cdate , $nextDate ); function dateRange( $begin , $end , $interval = null){ $begin = new DateTime( $begin ); $end = new DateTime( $end ); // Because DatePeriod does not the last date specified. $end = $end ->modify( '+1 day' ); $interval = new DateInterval( $interval ? $interval : 'P1D' ); return iterator_to_array( new DatePeriod( $begin , $interval , $end )); } $days = array_filter ( $dates , function ( $date ) { $day = $date ->format( "l" ); return $day === 'Tuesday' || $day === 'Thursday' || $day === 'Sunday' ; }); $arrayDays = array (); foreach ( $days as $date ) { $arrayDays [] = $date ->format( "d-m-Y" ); } //print Output print_r( $arrayDays ); |
This is show Tuesday, Thursday, Sunday date from 2 months.
No comments:
Post a Comment