Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

Modify this script so it aggressively sorts posts by creation date with newest first…

  1. User

    Modify this script so it aggressively sorts posts by creation date with newest first

  2. Assistant

    To aggressively sort the posts by creation date with the newest first, even when a post doesn't have a date specified in its YAML front matter, you can modify the getPostDate function to use the file's modification time as a fallback. Here's how you can modify your script:

    1. Update the getPostDate function:

    Modify it to return the file's modification time (filemtime) as a DateTime object when no valid date is found in the YAML front matter.

    function getPostDate($file) {
        $content = @file_get_contents($file);
        if ($content === false) {
            return null;
        }
        $content = str_replace("\r\n", "\n", $content); // Normalize line endings
    
        // Separate YAML front matter and Markdown content
        if (preg_match('/^---(.*?)---/s', $content, $matches)) {
            $yaml_content = trim($matches[1]);
            $yaml = parseYAML($yaml_content);
            if (is_array($yaml) && isset($yaml['date'])) {
                $dateStr = $yaml['date'];
                // Try parsing the date with time if available
                $dateFormats = ['Y-m-d H:i:s', 'Y-m-d H:i', 'Y-m-d'];
                foreach ($dateFormats as $format) {
                    $date = DateTime::createFromFormat($format, $dateStr);
                    if ($date) {
                        return $date;
                    }
                }
            }
        }
        // No valid date found, use file modified time
        $mtime = @filemtime($file);
        if ($mtime !== false) {
            return DateTime::createFromFormat('U', $mtime);
        }
        return null; // No valid date found
    }
    

    2. Adjust the date display in the post output:

    Since the dates may now include time, adjust the code that displays the date in each post to use the same getPostDate function. This ensures consistency in date formats and handles cases where the date comes from the file modification time.

    Replace this part:

    if (!empty($yaml['date'])) {
        $date = DateTime::createFromFormat('Y-m-d', $yaml['date']);
        if ($date) {
            echo '<p class="date">' . htmlspecialchars($date->format('F j, Y'), ENT_QUOTES, 'UTF-8') . '</p>';
        }
    }
    

    With:

    $date = getPostDate($file);
    if ($date) {
        echo '<p class="date">' . htmlspecialchars($date->format('F j, Y, g:i A'), ENT_QUOTES, 'UTF-8') . '</p>';
    }
    

    3. Ensure the date comparison in usort works with the updated getPostDate function:

    Your existing comparison function in usort should work correctly with the updated getPostDate function, as it now always tries to return a DateTime object.

    usort($all_files, function($a, $b) {
        $dateA = getPostDate($a);
        $dateB = getPostDate($b);
    
        // Both dates are null
        if ($dateA === null && $dateB === null) {
            return 0; // Treat as equal
        }
        // Only $dateA is null
        if ($dateA === null) {
            return 1; // $b is newer, so $a goes after
        }
        // Only $dateB is null
        if ($dateB === null) {
            return -1; // $a is newer, so $a goes before
        }
    
        // Both dates are valid, compare timestamps
        return $dateB <=> $dateA; // Sort in descending order
    });
    

    With these changes, the script now aggressively sorts all posts by their creation date (newest first), using the date from the YAML front matter if available, or the file's modification time as a fallback.

    Final Modified Script Snippet:

    // Function to extract the date from the YAML front matter
    function getPostDate($file) {
        $content = @file_get_contents($file);
        if ($content === false) {
            return null;
        }
        $content = str_replace("\r\n", "\n", $content); // Normalize line endings
    
        // Separate YAML front matter and Markdown content
        if (preg_match('/^---(.*?)---/s', $content, $matches)) {
            $yaml_content = trim($matches[1]);
            $yaml = parseYAML($yaml_content);
            if (is_array($yaml) && isset($yaml['date'])) {
                $dateStr = $yaml['date'];
                // Try parsing the date with time if available
                $dateFormats = ['Y-m-d H:i:s', 'Y-m-d H:i', 'Y-m-d'];
                foreach ($dateFormats as $format) {
                    $date = DateTime::createFromFormat($format, $dateStr);
                    if ($date) {
                        return $date;
                    }
                }
            }
        }
        // No valid date found, use file modified time
        $mtime = @filemtime($file);
        if ($mtime !== false) {
            return DateTime::createFromFormat('U', $mtime);
        }
        return null; // No valid date found
    }
    
    usort($all_files, function($a, $b) {
        $dateA = getPostDate($a);
        $dateB = getPostDate($b);
    
        // Both dates are null
        if ($dateA === null && $dateB === null) {
            return 0; // Treat as equal
        }
        // Only $dateA is null
        if ($dateA === null) {
            return 1; // $b is newer, so $a goes after
        }
        // Only $dateB is null
        if ($dateB === null) {
            return -1; // $a is newer, so $a goes before
        }
    
        // Both dates are valid, compare timestamps
        return $dateB <=> $dateA; // Sort in descending order
    });
    
    // ... (rest of your script)
    
    // In the loop where you display each post
    foreach ($files as $file) {
        // ... (your existing code to read and parse the file)
    
        // Get the date for display using the updated getPostDate function
        $date = getPostDate($file);
        if ($date) {
            echo '<p class="date">' . htmlspecialchars($date->format('F j, Y, g:i A'), ENT_QUOTES, 'UTF-8') . '</p>';
        }
    
        // ... (rest of your code to display the post)
    }
    

    Note:

    • This solution ensures that every post has a date associated with it for sorting purposes.
    • Posts without a date in their YAML front matter will use their file modification time, placing them appropriately in the sorted list.
    • Adjust the date format in date->format('F j, Y, g:i A') as needed to match your desired display format.