visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: php max date in an array


By: Bernard France  Date: 10/06/2007 08:49:20  English  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 English  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

EContact
browser fav
page generated in 291.457890 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page