Welcome Guest, Not a member yet? Register   Sign In
How Do You Calculate Date Ranges
#1

[eluser]Unknown[/eluser]
Are there standard functions in php (or sql) to help calculate date ranges. Each of my records have a start date. On a regular basis, I need to calculate how many are 0-6 months old, 7-12 months, 13-18 months, etc.

I also need to be able to take the current date and calculate the date that was 6 months, 12 months (which is easy), and 18 months earlier.

Are there functions or algorithms that do these calculations easily and correctly?
#2

[eluser]ggoforth[/eluser]
Hey there Chris,

I'm actually working with this kind of thing right now. There are a couple ways of working with dates. You can do it on the MySQL side of things, or on PHP. I find it easier to work with Dates in MySQL, but your mileage may vary.

In MySQL there are functions called DATE_ADD() and DATE_SUB() that allows you to do math with dates. It sounds like you will essentially be comparing today's date to a date in your data base. Give these functions a look and see if they fit for what your working on.

http://dev.mysql.com/doc/refman/5.1/en/d...n_date-add

Hope that helps.

Greg
#3

[eluser]jedd[/eluser]
Are the span() and other functions provided in the [url="http://ellislab.com/codeigniter/user-guide/helpers/date_helper.html"]CI Date Helper[/url] insufficient?
#4

[eluser]jalalski[/eluser]
[quote author="Chris Malumphy" date="1238843775"]Are there standard functions in php (or sql) to help calculate date ranges. Each of my records have a start date. On a regular basis, I need to calculate how many are 0-6 months old, 7-12 months, 13-18 months, etc.[/quote]

As mentioned, MySQL can do this. If you are willing to tie yourself to PHP 5.3 and higher, then there is the DateTime class, DateInterval and DatePeriod classes. See http://php.net/manual/en/book.datetime.php




Theme © iAndrew 2016 - Forum software by © MyBB