DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
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
)
Namaste UI

For any types of queries, you can contact us on info[at]namasteui.com.

Recent Posts

Your Complete Guide to the 461 Visa: Building a Life in Australia with Your New Zealand Partner

People usually stumble onto the 461 visa when they realise there’s no simple way to…

15 hours ago

Why Roarbank Is Transforming Everyday Banking in India

In today’s digital era, people look for convenience, transparency, and genuine rewards from their financial…

19 hours ago

Quality with Quantity: Importing LED Lights Can Save You Big Bucks

If you are planning to start a lighting business and looking for a supplier of…

2 days ago

11 Reasons Why Avoiding Plagiarism is Necessary for SEO and content marketing

If you are a copywriter or a content marketer, you will second my statement that…

4 days ago

6 Common Injuries After a Motorcycle Accident

Motorcycle accidents often lead to severe injuries because riders have minimal protection compared to drivers…

1 week ago

How Automation And SEO Can Improve Customer Experience

No business owner starts his or her business with the hope of making losses. Everyone…

1 week ago