note 102730 deleted from function.date by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: jaimthorn at yahoo dot com 

----

<?php

/**
 *  YA date converter. This function does NOT check if the supplied date is legal; it works according to the GIGO principle.
 *
 *  @param  string  $date       the input date.
 *  @param  string  $format_in  the format in which the input date is supplied.  ASSERT( strlen( $date ) === strlen( $format_in ) )
 *  @param  string  $format_out the format in which the output date has to be returned.
 *  @return string              the date in the format of $format_out.
 */
function YA_date_converter( $date, $format_in, $format_out )
{
    $len = min( strlen( $date ), strlen( $format_in ) );
    for( $i = 0; $i < $len; ++$i ) {
        if( false !== ( $pos = strpos( $format_out, substr( $format_in, $i, 1 ) ) ) ) {
            $format_out = substr( $format_out, 0, $pos ) . substr( $date, $i, 1 ) . substr( $format_out, $pos + 1 );
        }
    }

    return $format_out;
}

//  Feel free to figure out how it works. :-)
echo YA_date_converter( '02-03-2011', 'dd-mm-yyyy', 'yyyymmdd'                              ), PHP_EOL;     //  20110302
echo YA_date_converter( '02-03-2011', '!@-#$-%^&*', '*&^%$#@!'                              ), PHP_EOL;     //  11023020
echo YA_date_converter( '02/03-2011', 'DD MM YYYY', 'the year is YYYY, month MM, day DD'    ), PHP_EOL;     //  the/year-is 2011, month 03, day 02
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.