About this user

« Newer Snippets
Older Snippets »
8 total  XML / RSS feed 

add zeros to number or string

function to add zeros to number or string


function addZeros($numcount, $value, $where) {
    if ($value=="") echo "Error: value is missing";
    if ($numcount=="") echo "Error: numcount is missing";
    $newnum = "";
    $n = strlen($value);
    if ($where==">") $newnum .= $value;
    for ($i=0; $i<($numcount-$n); $i++) {
        $newnum .= "0";
    }
    if ($where=="<") $newnum .= $value;
    return $newnum;   
}

// usage:
echo addZeros(8, "122", "<");
// echos 00000122
echo addZeros(5, "2", ">");
// echos 200000

standard sql query in php

url to access: test.php?do=something


if ($_GET['do'] == 'something') {
    echo '
        ';$sql="SELECT * FROM table WHERE row=1";$query=mysql_query($sql);while($result=mysql_fetch_array($query)){echo'';}echo'
First Second Third
'.$result['first'].''.$result['second'].'
'; }

read url or file

read url or file

function readUrl($url) {
        $handle = fopen($url, "r");
        $contents = '';
        while (!feof($handle)) {
                $contents .= fread($handle, 8192);
        }
        fclose($handle);
        return $contents;     
}

make error div


function showError($errormsg, $exitstatus) {
        echo '';
        if ($exitstatus==1) exit();
}

random text generator



function RandGen($length) {
        $chars=array();
        for($i=48;$i<=57;$i++) {
                array_push($chars, chr($i));
        }
        for($i=65;$i<=90;$i++) {
                array_push($chars, chr($i));
        }
        for($i=97;$i<=122;$i++) {
                array_push($chars, chr($i));
        }
        while(list($k, $v)=each($chars)) {
        $k." -> ".$v."
"; } for($i=0;$i<$length;$i++) { mt_srand((double)microtime()*1000000); $passwd.=$chars[mt_rand(0,count($chars))]; } return $passwd; }

sql dates to european dates

converts sql dates to european dates and reverse.
2005-04-02 -> 02.04.2005
02.04.2005 -> 2005-04-02


function SqlToEuroDate($date) {
        $fd = substr($date, 8, 2)+0;
        $fm = substr($date, 5, 2)+0;
        $fy = substr($date, 0, 4);
        if ($fm<10) $fm = '0'.$fm;
        if ($fd<10) $fd = '0'.$fd;
        return  $newdate = $fd.'.'.$fm.'.'.$fy;
}

function EuroToSqlDate($date) {
        $fd = substr($date, 0, 2)+0;
        $fm = substr($date, 3, 2)+0;
        $fy = substr($date, 6, 4);
        if ($fm<10) $fm = '0'.$fm;
        if ($fd<10) $fd = '0'.$fd;
        return  $newdate = $fy.'-'.$fm.'-'.$fd;
}

how many days in year

calc days in year

function daysInYear($date) {
        $fd = (int)substr($date, 8, 2);
        $fm = (int)substr($date, 5, 2);
        $fy = (int)substr($date, 0, 4);
        return $days=date('z',mktime(0,0,0,$fm,$fd,$fy));
}

adjust date

adjust date. needed if you want to substract dates
example: date is 2005-12-31, and you want to get 1 month and 2 days greater date, then you just access adjustdate('2005-12-31', $years=0,$months=1,$days=2)

function adjustdate($date,$years=0,$months=0,$days=0) {
        $year=substr($date, 0, 4);
        $month=substr($date, 5, 2);
        $day=substr($date, 8, 2);
        return date("Y-m-d", mktime(0,0,0,$month+$months,$day+$days,$year+ $years));
}
« Newer Snippets
Older Snippets »
8 total  XML / RSS feed