2016-08-23 115 views
3

我有日期2016-02-17,我用DATE_FORMAT('2016-02-17', '%M, %D %Y')像這樣'february, 17th 2016'顯示它,但我想顯示february, seventeenth 2016,是Word 17th,請幫我如何顯示等單詞日期「二月十七2016」用mysql

+0

@ 1000111,您的評論是沒有意義的。 – Rahul

+0

對不起。第一次看到這個要求,我有點沮喪(:(),並且忍不住自己做了一個這樣的評論。@Rahul – 1000111

+0

@ 1000111,這個問題沒有錯,因爲我看到了,你應該考慮刪除你的評論 – Rahul

回答

0

寫入功能,並顯示答案

function convert_number_to_words($number) { 

$hyphen  = '-'; 
$conjunction = ' '; 
$separator = ' '; 
$negative = 'negative '; 
$decimal  = ' point '; 
$dictionary = array(
    0     => 'Zero', 
    1     => 'One', 
    2     => 'Two', 
    3     => 'Three', 
    4     => 'Four', 
    5     => 'Five', 
    6     => 'Six', 
    7     => 'Seven', 
    8     => 'Eight', 
    9     => 'Nine', 
    10     => 'Ten', 
    11     => 'Eleven', 
    12     => 'Twelve', 
    13     => 'Thirteen', 
    14     => 'Fourteen', 
    15     => 'Fifteen', 
    16     => 'Sixteen', 
    17     => 'Seventeen', 
    18     => 'Eighteen', 
    19     => 'Nineteen', 
    20     => 'Twenty', 
    30     => 'Thirty', 
    40     => 'Fourty', 
    50     => 'Fifty', 
    60     => 'Sixty', 
    70     => 'Seventy', 
    80     => 'Eighty', 
    90     => 'Ninety', 
    100     => 'Hundred', 
    1000    => 'Thousand', 
    1000000    => 'Million', 
    1000000000   => 'Billion', 
    1000000000000  => 'Trillion', 
    1000000000000000 => 'Quadrillion', 
    1000000000000000000 => 'Quintillion' 
); 

if (!is_numeric($number)) { 
    return false; 
} 

if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) { 
    // overflow 
    trigger_error(
     'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX, 
     E_USER_WARNING 
    ); 
    return false; 
} 

if ($number < 0) { 
    return $negative . convert_number_to_words(abs($number)); 
} 

$string = $fraction = null; 

if (strpos($number, '.') !== false) { 
    list($number, $fraction) = explode('.', $number); 
} 

switch (true) { 
    case $number < 21: 
     $string = $dictionary[$number]; 
     break; 
    case $number < 100: 
     $tens = ((int) ($number/10)) * 10; 
     $units = $number % 10; 
     $string = $dictionary[$tens]; 
     if ($units) { 
      $string .= $hyphen . $dictionary[$units]; 
     } 
     break; 
    case $number < 1000: 
     $hundreds = $number/100; 
     $remainder = $number % 100; 
     $string = $dictionary[$hundreds] . ' ' . $dictionary[100]; 
     if ($remainder) { 
      $string .= $conjunction . convert_number_to_words($remainder); 
     } 
     break; 
    default: 
     $baseUnit = pow(1000, floor(log($number, 1000))); 
     $numBaseUnits = (int) ($number/$baseUnit); 
     $remainder = $number % $baseUnit; 
     $string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit]; 
     if ($remainder) { 
      $string .= $remainder < 100 ? $conjunction : $separator; 
      $string .= convert_number_to_words($remainder); 
     } 
     break; 
} 

if (null !== $fraction && is_numeric($fraction)) { 
    $string .= $decimal; 
    $words = array(); 
    foreach (str_split((string) $fraction) as $number) { 
     $words[] = $dictionary[$number]; 
    } 
    $string .= implode(' ', $words); 
} 

return $string; 
} 

echo convert_number_to_words(17); 
5

你可以嘗試這樣的事情:

SELECT 
    CONCAT(
     MONTHNAME(NOW()), 
     ',', 
     SUBSTRING_INDEX(
      SUBSTRING_INDEX('First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st', ' ' ,DAY(NOW())), 
      ' ', 
      - 1 
     ), 
     ' ', 
     YEAR (NOW()) 
    ); 

注意:用您的日期字段替換NOW(),並根據需要更新SUBSTRING_INDEX('First Second....內的文本。

示範:

SET @sampleDate := '2016-02-17'; 

SELECT 
    CONCAT(
     MONTHNAME(@sampleDate), 
     ', ', 
     SUBSTRING_INDEX(
      SUBSTRING_INDEX('First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st', ' ' ,DAY(@sampleDate)), 
      ' ', 
      - 1 
     ), 
     ' ', 
     YEAR (@sampleDate) 
    ); 

See Demo

+0

爲什麼str_to_date!?! – Strawberry

+0

但它沒有給出確切的輸出,它顯示的數值不是字 –

+0

**但它沒有給出確切的輸出**請你可以更具體一些在這裏分享輸出,就像我說過的,你需要改變'SUBSTRING_INDEX'內的文本,如你想要的...... ..... SUBSTRING_INDEX('第一個第二個第三個第四個第五個第六個第七個第九個第九個第十個第11個第12個第13個第14個第15個第16個第17個第18個19日20日21日22日23日24日25日26日27日28日29日30日31日....'@bhanuavinash – 1000111

0

的上面的代碼說明什麼1000111貼:

 inner substring: 
       SELECT SUBSTRING_INDEX('First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth 11th 12th 13th 14th 15th 16th Seventeenth 18th 19th 20th 21st 22nd 23rd Twentyfourth 25th 26th 27th 28th 29th 30th 31st', ' ' ,DAY('2016-02-17')) 

     output: 
        First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth 11th 12th 13th 14th 15th 16th Seventeenth 18th 19th 20th 21st 22nd 23rd Twentyfourth 

     outer substring: 

       SELECT SUBSTRING_INDEX(
         SUBSTRING_INDEX('First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth 11th 12th 13th 14th 15th 16th Seventeenth 18th 19th 20th 21st 22nd 23rd Twentyfourth 25th 26th 27th 28th 29th 30th 31st', ' ' ,DAY('2016-02-17')), 
         ' ', 
         - 1 
        ) 

     output: 

       "Seventeenth " if count is -1 
       "first" if count is 1 
       "18th Seventeenth " if count is -2 
       "first second" if count is 2 

input: 

    SELECT 
    CONCAT(
     MONTHNAME('2016-02-17'), 
     ',', 
     SUBSTRING_INDEX(
      SUBSTRING_INDEX('First Second Third Fourth Fifth Sixth Seventh Eighth Ninth Tenth 11th 12th 13th 14th 15th 16th Seventeenth 18th 19th 20th 21st 22nd 23rd TwentyFourth 25th 26th 27th 28th 29th 30th 31st', ' ' ,DAY('2016-02-17')), 
      ' ', 
      - 1 
     ), 
     ' ', 
     YEAR ('2016-02-17') 
    ); 

output: 
    Feb, Seventeenth 2016 
相關問題