Categories: Php

Date range in PHP

Share

Date and time values are very essential in PHP as there are different time formats, time zones, daylight saving offsets.

By carefully with the date inputs you can format output however you like.

In the below function you can get range of dates:

function date_range( $start, $end, $step = '+1 day', $format = 'Y/m/d' )
{
$dates = array();
$current = strtotime( $start );
$end = strtotime( $end );
while( $current <= $end ) {
$dates[] = date( $format, $current );
$current = strtotime( $step, $current );
}
return $dates;
}
print_r( date_range( '2010/07/26', '2010/08/05') );

Read Also:  Php PHP date() and strtotime() return wrong months on 31st

You can get the result as below:

Array
(
[0] => 2015/12/01
[1] => 2015/12/02
[2] => 2015/12/03
[3] => 2015/12/04
[4] => 2015/12/05
[5] => 2015/12/06
[6] => 2015/12/07
[7] => 2015/12/08
[8] => 2015/12/09
[9] => 2015/12/10
)

Also, you can get full week interval:

print_r( date_range( '2015/12/01', '2016/01/10', '+1 week') );

And as a result you will get this:

Array
(
[0] => 2015/12/01
[1] => 2015/12/08
[2] => 2015/12/15
[3] => 2015/12/22
[4] => 2015/12/29
[5] => 2016/01/05
)

Recent Posts

Warm Comfort: Choosing the Best Hot Water Bottle for Cozy Nights

Introduction: As the chill of winter settles in or a bout of cold weather strikes,…

4 hours ago

One Location, Different Perspectives: The Allure of Dubai Marina Apartments

Nestled along the glittering waterfront of the City of Gold, Dubai Marina beckons to discerning…

4 hours ago

Role Of Gojek Clone In Growth Campaigns For Your Business

When was the last time you heard that a clone app like Gojek could efficiently…

10 hours ago

How to Optimize Your E-Commerce Pages and Improve UX?

The e-commerce market is growing and evolving at a rapid pace. More and more people…

21 hours ago

Overview of Reputation, Services, and Features of IplWin

IplWin stands as a reliable and enthralling platform for Indian punters, offering a captivating blend…

3 days ago

Blogging Brilliance: Driving Traffic and Engagement with Quality Content

Introduction In today's online age, consumers are constantly bombarded with information. They crave valuable content…

3 days ago