I'm working on a project right now and I'm facing an issue that is about to freak me out.

Basically I had to display information of some courses, in order to do so, I create a custom field called "CourseInfo", that array includes, date, place, and a link to sign in.

So the user create that custom field as long as he needs, cause courses can be infinites, however I need to order the output based on the course date and not based on the last custom field created.

Since the course date includes month and the year, is there a change to order it basd on the date?

I would appreciate any help!


UPDATE****** ------------------------------------------

Basically I'm using this code, maybe it will help

 <?php $limit = 1; $courses = array_reverse($courses); foreach ($courses as $course) {
                            $course = nl2br($course);
                            list($date,$city,$link) = explode('<br />',$course);

 echo "<ul>";
 echo "<li>$date</li>";
 echo "<li>$city</li>";
 echo "<li><a href='$link'>Inschrijven</a></li>";
 echo "</ul>";

 }  ?>

And the page is http://www.otoacademie.nl/dev/rijinstructeur-auto-b/

Notice there is not order.

UPDATE===================================================== update dates format

enter image description here

  • I'm a little confused. Can you give us an example of the data contained in $courses? – v0idless Nov 1 '11 at 22:19
  • sure, I'm updating the post with a ss of the custom fields =) – andresmijares Nov 1 '11 at 22:23
  • can you do a echo "<pre>" . print_r($courses, true) . "</pre>" and post that output? I'd like to see how the data is actually represented in the array. – v0idless Nov 1 '11 at 22:33
  • I just set it in otoacademie.nl/dev/rijinstructeur-auto-b Just click "Alle opleidingsdata en locaties zien" and you will see the output – andresmijares Nov 1 '11 at 23:00
  1. Multiline is not an array (rant)
  2. You need to use standard dates for this to work DD/MM/YYYY (first row)
  3. Use the PHP function usort() to sort the fields
  4. Use strtotime() to convert that date to a timestamp and date_i18n() to output it

If you need an actual example, change the date formats to what I've shown you above, update the screenshot and I'll give you the code. First line must be DD/MM/YYYY.


// Demo set (replace with you custom fields)
$courses = array(
    '27/10/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
    '19/10/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
    '17/01/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
// Convert the courses to objects and translate the dates
$sorted_courses = array(); // Holds objects for sorting
foreach($courses as $course){
    $course = nl2br($course);
    // Replace <br /> with \r\n
    $course = preg_replace('~<br[\s]*/[\s]*>~i', PHP_EOL, $course);
    // Split by \r\n and trim each entry
    list($date, $city, $link) = array_map('trim', preg_split('~[\r\n]+~', $course));
    // Validate the date format [ :) ]
    if(!preg_match('~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~', $date, $slices)) continue;
    // Extract date elenents
    list($match, $day, $month, $year) = $slices;
    // And now create the object
    $sorted_courses[] = (object)array(
        'time'  => $time = mktime(1, 1, 1, $month, $day, $year),
        'date'  => $date = date_i18n('d F Y', $time),
        'city'  => $city,
        'link'  => $link,
// Sort elements ascending (using callback)
usort($sorted_courses, function($item1, $item2){
    // Compare the timestamps and sort ascending
    return $item1->time > $item2->time;
// Got through the sorted courses and display them
foreach($sorted_courses as $sorted_course){
    echo "<ul>";
        echo "<li>{$sorted_course->date}</li>";
        echo "<li>{$sorted_course->city}</li>";
        echo "<li><a href='{$sorted_course->link}'>Inschrijven</a></li>";
    echo "</ul>";
  • just update it :) hope u can help me! thanks a ton! – andresmijares Nov 2 '11 at 16:54
  • Check it out :) Have fun! – EarnestoDev Nov 2 '11 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.