Welcome Guest, Not a member yet? Register   Sign In
preg_replace help needed
#4

[eluser]jedd[/eluser]
[quote author="CISCK" date="1257020493"]
I guess I just wanted something more dynamic ...
[/quote]

Well, sure, if you want something that takes up more space, uses more CPU to run, and whose function is less obvious to the reader .. you don't have to use a simple and effective solution.

Here's a function I wrote a while ago that includes a 24hour conversion similar to what you're trying to do. In this case I wanted to keep the date portion of a string, and round the hours/minutes/seconds portion to the nearest hour.

Code:
// ------------------------------------------------------------------------
/**
* Pretty date
*
* Takes one parameter - an ISO8601 formatted date - and returns it
* in slightly more human readable form.
*
* eg. 2009-05-31T16:42:07  ====>  2009-05-31 , 5pm
*
* @param    $date_in    string (iso8601 date)
* @return    string
**/
function  pretty_date ( $date_in )  {
    if (strlen ($date_in) != 19)
        return $date_in;                // return in confusion!

    $hh = substr ($date_in, 11, 2);
    $mm = substr ($date_in, 14, 2);

    if ($mm > 30)
        $hh++;

    if ( ($hh > 23) OR ($hh == '00'))
        $hh_string = "midnight";
    else
        if ($hh > 12)
            $hh_string = ($hh - 12) ."pm";
        else
            if ($hh == 12)
                $hh_string = "midday";
            else
                $hh_string = $hh ."am";

    $output = substr ($date_in, 0, 10) . nbs(1) ."<font class=\"date_tilde\">~</font>". nbs(1) . $hh_string;

    return $output;
    }  // end-function  pretty_date ()


Messages In This Thread
preg_replace help needed - by El Forum - 10-31-2009, 07:53 AM
preg_replace help needed - by El Forum - 10-31-2009, 08:46 AM
preg_replace help needed - by El Forum - 10-31-2009, 09:21 AM
preg_replace help needed - by El Forum - 10-31-2009, 10:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB