Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
depending on how your dates are stored, you may be able to use strtotime() which will parse many, many date formats into simple UNIX timestamps. which should be easier to sort.
 
sonofslim said:
depending on how your dates are stored, you may be able to use strtotime() which will parse many, many date formats into simple UNIX timestamps. which should be easier to sort.
Hmmm, I don't think I'm quite to that point yet. I can't get it to sort based on anything other than the filename.
 
Here's the thing: I'm a complete moron. No matter how many times I read through the manuals over at php.net they make absolutely zero sense to me. None. Not a single bit.

I started to think some kind of 2-dimensional array might be what the doctor ordered, but I ran into a brick wall at php.net (see my above comment).

I'm hopeless.
 
brian,

where are you getting the dates from? they must be stored somewhere, in some format, right? tell us more about that and we may be able to give you something more concrete.

what i was getting at earlier was: by converting your dates to a timestamp* you make it much easier to sort. one way to do it would be to plunk your times (just as they're stored, ie 'Apr 05 2004') and their corresponding image source or url into an array. loop through the array and call strftime() on each date, which will return a UNIX timestamp -- a simple, single string of numbers. replace the original time with the timestamp. sort this new array (timestamp/SRC) by timestamp, and use it to spit out the images in the desired order. you can then use strftime() to return the timestamps to their original format. the exact useage in your case would be:
Code:
strftime('%b %d %Y', $timestamp)
a better way to do it would be with usort(), but i leave that exercise to the reader.

*basically, 00:00:00 on Jan. 1, 1970 was UNIX timestamp 0. right now (about 13:54, May 13, 2004) it's timestamp 1084470888. you're just counting seconds elapsed since midnight, new year's eve, 1970. which makes it very easy to sort stuff.
 
Cool, I'll probably end up doing something like that. I screwed up before because I thought it'd be as simple as adding in a line or two of code, but it's going to be a little more than that (but not too much more hopefully). I was going to grab the date by using the "filemtime" function. That's how I grab the date to display it on the page/gallery.

Also, setting up a 2D array will probably give me some problems. Like I said, I planned on reading the filename and date of each picture into an array and going from there, but I hit a snag when I tried reading the documentation.

I'll be giving all this a (more in-depth) try sometime after this weekend though. Thanks.
 
OK, I'm starting to play with it... here's how I get my picture names and dates:

Code:
while (($file = readdir($handle))!==false) {
  //	filter for jpg files... 	
    if (substr($file,-4) == ".jpg"){
      $pics[$count] = $file;
      $picfile = $fullpath."/".$pics[$count];
      $dates[$count] = filemtime($picfile);
      $count++;
    }
}
I don't know if it's the most efficient way... I think this is code I've grabbed from previous scripts... it works now as is and it's grabbing the date in the unix timestamp format.

Now, I could just have it sort $dates, but then when I go to output it how do I associate the the filename ($pics) with $dates? I'm having a mental block right now but I'll play around with it to see if I can come up with something.
 
try using named key/value pairs in your array. your array right now is going to look like this:

$dates[0] = (timestamp)
$dates[1] = (timestamp)
$dates[2] = (timestamp)
$dates[3] = (timestamp)

and so on.

but if you named your keys as well, you'd have an associative array that matches filenames with timestamps. something like the following, maybe. (and please excuse my hybrid english/php; i'm just trying to get an example across.)
Code:
while(you've got some files left)
if(this is a jpg file)
{
     // get this particular $filename
     $dates[$filename] = filemtime($filename);
}

which will create an array something like this:
$dates["file1.jpg"] = (timestamp)
$dates["file2.jpg"] = (timestamp)
$dates["thatpictureofbrianwithhispantsoff.jpg"] = (timestamp)

and so on. now you can sort the array on the timestamp value (using sort($dates)) and just step through the array in sequence, to retrieve the filenames. of course, you'll have to retrieve the key rather than the value. here's one way to do it:
PHP:
foreach($dates as $key => $value)
{
     // will step through the array in order
     // and return two variables, $key and $value,
     // correlating to filename and timestamp, 
     // for each pair. 
     echo '<img src="'.$key.'">'; // or whatever
}

or you could reverse the key/value when you create the $dates array:
PHP:
$dates[filemtime($filename)] = $filename
so you'd get an array in this format:
$dates["timestamp1"] = "file1.jpg";
then you could use ksort($dates) to sort the array on the key, and just retrieve the values as usual.

does that make sense?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.