<?php
// add zeros to a number at left or right side.
function add_zeros_to_number( $number, $number_of_zeros, $zeros_position="left"){
// check if number is negative
$is_negative = FALSE;
if ( strpos($number , '-') !== FALSE ){
$is_negative = TRUE;
$number = substr($number, 1);
}
if($zeros_position == "right"){
$r = str_pad($number, $number_of_zeros, "0", STR_PAD_RIGHT);
}else{
$r = str_pad($number, $number_of_zeros, "0", STR_PAD_LEFT);
}
if( $is_negative ){
return "-".$r;
}else{
return $r;
}
}
// how to use
$number = -333; // Desire number
$number_of_zeros = 4; // number of zeros [ your number length + zeros ]
$position = "right"; // left or right . default left
echo $result = add_zeros_to_number($number, $number_of_zeros, $position);
// output
// -333 => -3330 left
// -333 => -0333 right