Wednesday, January 16, 2013

0

php - format number with comma and decimal


Do you want to show your user a formatted number with comma and decimal? You come to the right place. Here's how.

function getNumberWithCommaAndDecimal( $given_number ){

// if the number given has decimals
if (strpos($given_number, '.') !== false) {

  // count the number of decimals
$decimal_places = strlen( end( explode( ".", $given_number ) ) );

  // get the formatted number
$result = number_format( $given_number, $decimal_places );

}

 // the number given has no decimal places
 // just get the formatted number with two decimal places
 else{
$result = number_format( $given_number, 2 );
}

return $result;
}

How to use?

// will output 1,000,000.00
getNumberWithCommaAndDecimal(1000000);
More

Tuesday, December 11, 2012

0

PHP Number Format

I wanted to format a price number with two decimal places, for example 1245, here's what I did.

//code sample
number_format(1245, 2, '.', ',');

//output is 1,245.00

//First parameter (1245) is our input number
//Second paramter (2) is the number of decimal places you want
//Third parameter ('.') is the character for decimal separator
//Fourth parameter (',') is the character for thousands separator
More

Monday, December 10, 2012

0

MySQL Query With Multiple Left Join

Here is a query I used to (LEFT) join multiple tables:

SELECT 
fe.fex_uid, 
fe.ca_id,
fet.product_group_id, 
pg.name,
fet.type, 
fet.value, 
q.weight,
DATE_FORMAT( fe.timestamp,  '%Y-%m-%d' ) as fe_date

FROM 
field_executions fe
     LEFT JOIN field_execution_items fet
          ON fe.fex_uid = fet.fex_uid
     LEFT JOIN qualifiers q
          ON fet.value = q.id
     LEFT JOIN product_groups pg
          ON fet.product_group_id = pg.id

WHERE 
fe.company_id = '000002' AND
fe.ca_id = 3 AND
date_format(fe.timestamp,'%Y-%m-%d') LIKE '2012-12%'

ORDER BY 
fe.fex_uid,
fet.product_group_id
More

Monday, February 13, 2012

0

PHP Get Current Datetime

I used this code when I want to save a date/time value for my "created" table field.

//you have to set it where it is appropriate for you
date_default_timezone_set('Asia/Manila'); 

$created = date('Y-m-d h:i:s');

echo $created;

Output:
2012-02-14 10:08:31


Sample Use:
$sql = "insert into 
your_table
( 
    created,
    name
)
values 
( 
    '" . $created . "',
    '" . escapeStr( $name ) . "'
)";

By the way, you can also use NOW()
VALUES
(
    NOW(),
    '" . escapeStr( $name ) . "'
)";

But it seemed like it doesn't work properly for me.

Value stored using $created:
2012-02-14 10:08:31

Value stored using NOW():
2012-02-14 02:08:53

In my case, using the $created value is just fine. I haven't tried setting proper timezone for using NOW(), anyone? :)
More
Powered by Blogger.