note 102730 deleted from function.date by danbrown
| From: | danbrown@php.net | Date: | Wed, 02 Mar 2011 17:03:03 +0000 |
| Subject: | note 102730 deleted from function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177502@lists.php.net to get a copy of this message | ||
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