tags:

views:

87

answers:

5

What do you say about the following Date Function ?

The following code is from a shopping cart.


function va_date( $mask = "", $date = "" )
{
    global $months;
    global $short_months;
    global $weekdays;
    global $short_weekdays;
    $formated_date = "";
    if ( !is_array( $date ) )
    {
        $date = is_numeric( $date ) ? va_time( $date ) : va_time( );
    }
    if ( !is_array( $mask ) )
    {
        $mask = parse_date_format( $mask );
    }
    if ( is_array( $mask ) )
    {
        $i = 0;
        for ( ; $i < sizeof( $mask ); ++$i )
        {
            switch ( $mask[$i] )
            {
            case "YYYY" :
                $formated_date .= $date[YEAR];
                break;
            case "YY" :
                $formated_date .= substr( $date[YEAR], 2 );
                break;
            case "WWWW" :
                $formated_date .= $weekdays[intval( date( "w", va_timestamp( $date ) ) )][1];
                break;
            case "WWW" :
                $formated_date .= $short_weekdays[intval( date( "w", va_timestamp( $date ) ) )][1];
                break;
            case "MMMM" :
                $formated_date .= $months[intval( $date[MONTH] ) - 1][1];
                break;
            case "MMM" :
                $formated_date .= $short_months[intval( $date[MONTH] ) - 1][1];
                break;
            case "MM" :
                $formated_date .= strlen( $date[MONTH] ) == 2 ? $date[MONTH] : "0".$date[MONTH];
                break;
            case "M" :
                $formated_date .= intval( $date[MONTH] );
                break;
            case "DD" :
                $formated_date .= strlen( $date[DAY] ) == 2 ? $date[DAY] : "0".$date[DAY];
                break;
            case "D" :
                $formated_date .= intval( $date[DAY] );
                break;
            case "HH" :
                $formated_date .= strlen( $date[HOUR] ) == 2 ? $date[HOUR] : "0".$date[HOUR];
                break;
            case "H" :
                $formated_date .= intval( $date[HOUR] );
                break;
            case "hh" :
                $formated_date .= get_ampmhour( $date ) == 2 ? get_ampmhour( $date ) : "0".get_ampmhour( $date );
                break;
            case "h" :
                $formated_date .= intval( get_ampmhour( $date ) );
                break;
            case "mm" :
                $formated_date .= strlen( $date[MINUTE] ) == 2 ? $date[MINUTE] : "0".$date[MINUTE];
                break;
            case "m" :
                $formated_date .= intval( $date[MINUTE] );
                break;
            case "ss" :
                $formated_date .= strlen( $date[SECOND] ) == 2 ? $date[SECOND] : "0".$date[SECOND];
                break;
            case "s" :
                $formated_date .= intval( $date[SECOND] );
                break;
            case "AM" :
                $formated_date .= get_ampm( $date );
                break;
            case "am" :
                $formated_date .= strtolower( get_ampm( $date ) );
                break;
            case "GMT" :
                $formated_date .= isset( $date[GMT] ) ? $date[GMT] : "";
                break;
            default :
                $formated_date .= stripslashes( $mask[$i] );
            }
        }
    }
    else
    {
        $formated_date = $date[YEAR]."-".$date[MONTH]."-".$date[DAY]." ".$date[HOUR].":".$date[MINUTE].":".$date[SECOND];
    }
    return $formated_date;
}


+1  A: 

Well, this is really function.

Case could be replaced with table... it will be more readable...

Mike Chaliy
A: 

I'd say it has no documentation, so I don't really know what to say.

I'd also say its reimplemented strftime or date and you should also look at sprintf to replace all that strlen( $date[MONTH] ) == 2 ? $date[MONTH] : "0".$date[MONTH]; stuff you've got.

Schwern
+1  A: 

At first:

It's using global variables. I don't find it an elegant solution. This way, the function is dependend on external state, which makes it harder to debug.

Ikke
A: 

If your PHP installation is >= 5.1.0, I would say, you'd be better off using strptime and, if absolutely necessary, a translation function for your formatting strings (the $mask).

Cheers,

Boldewyn
A: 

I'd say that if you find yourself writing a function to format a date, you probably are re-inventing the wheel.

JohnFx
Can you recommend any pre-built functions or libraries for Date ?
Ibn Saeed
How about this one?http://www.w3schools.com/PHP/php_date.asp
JohnFx