ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Convert URL to Date Stamp

December 31, 2007 10:27am

Subscribe [3]
  • #1 / Dec 31, 2007 10:27am

    bjsteiger

    87 posts

    Happy New Years Eve…

    I have what’s probably a simple question but I can’t seem to remember how to do this.

    Basically my URL string looks like….

    http://www.domain.com/events/2008/10/23/

    But in the body of the page, I want to take that URL segments and convert it to:

    October 23rd

    Help…?

  • #2 / Dec 31, 2007 1:03pm

    bjsteiger

    87 posts

    Okay…. I’ve been playing around with this and some PHP code and I can’t get it to work properly - I’m using it with Mr. Wilson’s Repeet Calendar plugin. Basically I need a way to display a page header, so its outside of the Repeet code unless there is another way to handle this.

  • #3 / Dec 31, 2007 3:42pm

    OrganizedFellow

    435 posts

    Perhaps something like this:

    {if segment_3 == "1"}
        Jan.
    {/if}
    {if segment_3 == "2"}
        Feb.
    {/if}
    {if segment_3 == "3"}
        Mar.
    {/if}
  • #4 / Dec 31, 2007 4:13pm

    Mr. Wilson

    131 posts

    Hi bjsteiger,

    There are a bunch of ways to do it. One approach, using PHP (parsed on output):

    <?php
    
    //
    // Format a date based on values taken from the URL
    //
    
    // Fetch the year and ensure it's legit
    $year = '{segment_2}';
    if (!is_numeric($year) || $year < 1900 || $year > 2100)
    {
        $year = date('Y');
        // If the year isn't legit, we assume the month and day aren't legit either
        $month = 1;
        $day = 1;
    }
    else
    {
        // Fetch the month and ensure it's legit
        $month = '{segment_3}';
        if (!is_numeric($month) || $month < 1 || $month > 12)
        {
            $month = date('m');
            // If the month isn't legit, we assume the day isn't legit either
            $day = 1;
        }
        else
        {
            // Fetch the day and ensure it's legit
            $day = ' {segment_4}';
            $days_in_month = date('t', mktime(12, 0, 0, $month, 1, $year));
            if (!is_numeric($day) || $day < 1 || $day > $days_in_month)
            {
                $day = 1;
            }
        }
    }
    
    // Output something like 'October 23rd'
    echo date('F jS', mktime(12, 0, 0, $month, $day, $year));
    
    ?>
  • #5 / Jan 02, 2008 12:02pm

    bjsteiger

    87 posts

    Mr. Wilson…

    Thanks for this - I was actually on this same path but I didn’t include the test and my “mktime” kept bombing out because some pages don’t include a date (which is okay).

    Thank you again,
    bjs

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases