Domanda

I'm using a loop to the information from my database and I want to get the difference between the dates within this loop. I can show you how it looks like at the moment (the SQL query with the loop):

$get_photos = "SELECT * FROM photos ORDER BY exif_taken DESC";

foreach($sql->query($get_photos) AS $photo) {
    echo $photo['exif_taken'];
}

You can see the loop in action here (removed). The first 2 images you can see in the loop on the website, are taken 2013-10-20. The next four images are taken 10 days before (2013-10-10). The two next images are taken 2013-06-08 and the last image are taken 2013-06-06.

I want to insert a small cap between the images upon date difference. But to do this, I need to get the date difference between the images and it's that I don't know how to do. Let me show you what I mean.

image-1 image-2 new date - small cap image-3 image-4 image-5 image-6 new date - small cap image-7 image-8 new date - small cap image-9

How can I accomplish this?

È stato utile?

Soluzione

Do you want to show the difference between the photo and the one before it? You'll need to keep an array of dates you've already outputted:

$dates = array();

foreach($sql->query($get_photos) AS $photo) {
    echo $photo['exif_taken'];
    $original_date = $photo['exif_taken'];
    if(!in_array($original_date, $dates))
        $dates[] = $original_date;

    // get the difference between this and the previous
    if(count($dates) > 1) {
        $last_key = count($dates) - 1;
        $previous_date = $dates[$last_key - 1];
        $timestamp_difference = strtotime($original_date) - strtotime($previous_date);
        $difference_in_days = floor($timestamp_difference/(60*60*24));
        echo $difference_in_days . ' days after previous entry';
    }
}

If your exif_taken variable contains more than just dd/mm/yyyy format, you'll probably want to make it dd/mm/yyyy so that it will match up in the array:

$original_date = date('d/m/Y', strtotime($photo['exif_taken']));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top