WEB언어/PHP

php money_format 변환 - 미국 형식 화폐 / MySQL 일자포멧 / 요일확인

saltdoll 2014. 10. 23. 15:06
반응형

화폐단위로 표시하기


PHP에는 여러나라의 화폐 단위로 변경이 가능한 함수가 있다.

참고: http://php.net/manual/en/function.money-format.php

// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY'en_US');
echo 
money_format('%(#10n'$number) . "\n";

// ($        1,234.57)



함수로 만들기

// ### Number change Money Format ##########
function getMoneyFormat($m_str)
{
setlocale(LC_MONETARY, 'en_US');
$m_str = money_format('%(#10.0n', $m_str);
return $m_str; }

// ### 함수 호출 #######

getMoneyFormat($t_amount);



또 다른 방법 => number_format() 함수

(윈도우 IIS+PHP의 경우money_foramt함수가 정의되지 않는다는 에러가 발생할 수 있습니다.)

 number_format ( float $number [, int $decimals = 0 ] ) : string 

또는

number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," ) : string


함수로 만들기

 // ### Number change Money Format ##########

function getMoneyFormat($m_str)
{
$m_str = number_format($m_str, 2, '.', ',');
$m_str = '$'.$m_str;
return $m_str;
}


$number = 1234.5678;

$en_number = getMoneyMormat($number); 

// $1,234.57


<?php

$numbers = array(0.0010.0020.0030.0040.0050.0060.0070.0080.009);

foreach ($numbers as $number)

    print $number."->".number_format($number2'.'',')."<br>";

?>

0.001->0.00

0.002->0.00

0.003->0.00

0.004->0.00

0.005->0.01

0.006->0.01

0.007->0.01

0.008->0.01

0.009->0.01



참고: number_format API Documentation







요일 확인하기

http://php.net/manual/en/function.date.php

http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=qna_function&wr_id=331063

// 요일 확인

//$today = "2011-06-28";

echo date('w', strtotime("2014-10-5"));


//결과 (일요일)

0


string date ( string $format [, int $timestamp = time() ] )





MySQL 일자 포멧 만들기

http://www.w3schools.com/sql/func_date_format.asp


Syntax :

DATE_FORMAT(date,format)


Example :

DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')

DATE_FORMAT(NOW(),'%d %b %Y %T:%f')


Result :

Nov 04 2008 11:45 PM
11-04-2008
04 Nov 08

04 Nov 2008 11:45:34:243





 DATE_FORMAT(date,format)

Formats the date value according to the format string.

The following specifiers may be used in the format string. The % character is required before format specifier characters.

SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th1st2nd3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week; WEEK() mode 0
%uWeek (00..53), where Monday is the first day of the week; WEEK() mode 1
%VWeek (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%vWeek (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal % character
%xx, for any x not listed above

Ranges for the month and day specifiers begin with zero due to the fact that MySQL permits the storing of incomplete dates such as '2014-00-00'.

출처: https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

반응형
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)