-2

I want to get date by year in php. (Ex. from 1995 to 2013). can anyone please help me on this?

01-01-1995 02-01-1995 . . . . . 31-01-2013

Mohan
  • 1
  • 1
  • 2
    A year isn't a specific date. What data do you have and what do you need? – jwhb Oct 06 '14 at 09:13
  • Do you mean that you need a list of all the dates in a year? – Mark Baker Oct 06 '14 at 09:16
  • possible duplicate of **[PHP: Return all dates between two dates in an array](http://stackoverflow.com/a/21073726/67332)** – Glavić Oct 06 '14 at 12:50
  • possible duplicate of [PHP: Return all dates between two dates in an array](http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – vascowhite Oct 07 '14 at 13:18

1 Answers1

1

I think what you're trying to do is get all dates between for example 1995 and 2013. This is called a period. PHP has some nice tools to do this. For example try looking at a DatePeriod. Your code could be like this:

$period = new DatePeriod(
     new DateTime('1995-01-01'),
     new DateInterval('P1D'),
     new DateTime('2013-01-31')
);

This gives you a DatePeriod object that is filled with all dates using Traversable so you can iterate over it using a foreach loop. The interval you need is Per 1 Day (P1D). Next you need to fill your own array with the desired format. Like this for example:

foreach( $period as $date) { $array[] = $date->format('Y-m-d'); }

Or simply print the dates like so:

foreach( $period as $date) { echo $date->format('Y-m-d') . "<br />"; }

I hope this helps. Info found at: PHP: Return all dates between two dates in an array

Community
  • 1
  • 1
tonsmets
  • 92
  • 3