![]() |
How Do You Calculate Date Ranges - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: How Do You Calculate Date Ranges (/showthread.php?tid=17413) |
How Do You Calculate Date Ranges - El Forum - 04-04-2009 [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? How Do You Calculate Date Ranges - El Forum - 04-04-2009 [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/date-and-time-functions.html#function_date-add Hope that helps. Greg How Do You Calculate Date Ranges - El Forum - 04-04-2009 [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? How Do You Calculate Date Ranges - El Forum - 04-04-2009 [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 |