1
 class Photos 
{
private $photos = array();

function add_photo($filename, $date, $lat, $long) 
{
  $this->photos[] = array('filename' => $filename, 'date' => $date, 'lat' => $lat,  'long' => $long);
    return $this;
}

function get_all() 
{
   return json_encode($this->photos);
}

function get_N($n) 
{
    return json_encode(array_slice($this->photos, 0, $n));
}

}

I would like another function in my class that returns some of the arrays with a certain date. The date is extracted from photos using the exif_read_data function. They look something liek this: 2011:04:01 16:12:23. The function that im looking for should return all the photos from a certain date. So i would like to know how i make the function return, for example, all photos with a datestamp looking like 2011:04:01 xx:xx:xx. Hope you understand what i mean. Thanks in advance!

3 Answers 3

1

This should do it:

class Photos 
{
    private $photos = array();

    function add_photo($filename, $date, $lat, $long) { /* ... */ }

    function get_all() { /* ... */ }

    function get_N($n) { /* ... */ }

    function get_by_date($date)
    {
        $result = array();

        foreach ($this->photos as $photo)
        {
            if (strpos($photo['date'], $date) === 0)
            {
                $result[] = $photo;
            }
        }

        return $result;
    }
}

Photos::get_by_date('2011:04:01');

You may also want to look at the array_filter() function.


function get_all_dates()
{
    $result = array();

    foreach ($this->photos as $photo)
    {
        $result[substr($photo['date'], 0, 10)] = null;
    }

    return array_keys($result);
}

function get_all_dates_with_count()
{
    $result = array();

    foreach ($this->photos as $photo)
    {
        $date = substr($photo['date'], 0, 10);

        if (empty($result[$date]))
        {
            $result[$date] = 0;
        }

        ++$result[$date];
    }

    return $result;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Good note about the array_filter() function, but that would effectively be the same as just doing what you have here. You'll end up writing a function comparing the date anyway.
@Compeek: In PHP 5.3 you can define lambda functions, but yeah. I just mentioned because the OP might want to perform similar filters on his data. :)
Never hurts to give an answer with bonus info. :)
hmm, followup question: is there any way to make a function that returns all the dates that contains any photos?
btw, how do i count the number of photos for each date? i would like to return a count with each date.
0
function get_by_date($date) {
    $date = strftime('%D', strtotime($date));
    $photos = array();
    foreach($this->photos as $photo) {
        if(strftime('%D', strtotime($photo['date'])) == $date) {
            $photos[] = $photo;
        }
    }
    return $photos;
}

Comments

0

I would simply check if the first 10 characters are the same:

public function getByDate($date) {
    $return = array();
    foreach ($this->photos as $photo) {
        if ($date == substr($photo['date'], 0, 10)) {
            $return[] = $photo;
        }
    }

    return $return;
}

If you are on PHP 5.3 you could do this using array_filter and a Closure:

public function getByDate($date) {
    return array_filter($this->photos, function($photo) use($date) {
        return $date == substr($photo['date'], 0, 10);
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.