Languages :: PHP :: php max date in an array |
|||
| By: Bernard |
Date: 10/06/2007 08:49:20 |
Points: 20 | Status: Answered Quality : Excellent |
|
Using php, I would like to have the max date in an array how ? |
|||
| By: VGR | Date: 11/06/2007 16:52:06 | Type : Answer |
|
| well, it depends on your data ;-) if the dates (or datetimes) are stored in the standard ISO format, then it's a piece of cake. If they are not, then it's just a matter of adding function calls to convert the dates so that they can be compared. You must be warned also that it is ***a lot*** easier and faster, given your data comes out from a DB, to let the DB engine do the computation for you. For instance, if I had to get the max(date) from a set of rows, I wouldn't read the data into memory and then use PHP to parse them to get the maximum. I would ask MySql what is the max(date), simply. Sometimes, the best solutions look like stupid ideas at first. [not so stupid : even with overhead, calling MySql once is a lot faster than the CPU time required to sort/parse/extract max() from a huge memory array ; this depends on the number of rows in your dataset, of course] This caveat posed, here is an example of code to extract the max of a dates array in PHP : // requirement : dates are stored in ISO YYYY-MM-DD HH:MM:SS format // if not, add date() function calls to convert - see www.php.net/manual/en/ // // thearray[] contains the data // $maxis=''; foreach ($thearray as $somevalue) if ($somevalue>$maxis) $maxis=$somevalue; echo "the max date is $maxis "; |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








