Caputo's blog

Informatica, tecnologia, programmazione, fai da te, papercraft e papertoy

Funzioni utili per PHP

Maggio 9th, 2009 by Giovanni Caputo

  1. Generare password random

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?php
    
    function generatePassword($length) {
        $character = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $password = "";
        for($i=0;$i<$length;$i++) {
            $password .= $character[rand(0, 61)];
        }
        return $password;
    }
    
    echo generatePassword(7);
    
    ?>
  2. Calcolare anni a partire dalla data di nascita

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
    
    $testcase = array("12-03-1985", "15-01-1987", "01-01-2000");
    
    function getAge($year) {
        return floor(abs(strtotime('now') - strtotime($year))/31536000);
    } 
    
    foreach($testcase as $test) {
        echo "Born in $test, approximately age is " . getAge($test)." years<br/>";
    }
    ?>
  3. Ottenere indirizzo IP dei visitatori

    1
    2
    3
    4
    5
    <?php  
    
    echo "Your IP address is " . $_SERVER['REMOTE_ADDR'];  
    
    ?>
  4. Ottenere l’estensione di un file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
    
    $testcase = array("sample.txt", "sample.jpg", "sample.case.txt");
    
    function extension($filename){
        return substr(strrchr($filename, '.'), 1);
    }
    
    foreach($testcase as $test) {
        echo "Extension from $test is " . extension($test) . "<br/>";
    } 
    
    ?>
  5. Ordinamento di un array

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <?php
    
    $data_list = array("lychee", "pineapple", "apple", "mango", "strawberry", "banana", "orange", "grape", "guava");
    
    sort($data_list);
    foreach($data_list as $data) {
        echo $data . "<br/>";
    } 
    
    ?>
  6. Tempo trascorso da una certa data

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <?php
    
    $testcase = array("12-03-1985", "15-01-2007", "04-04-2009", "01-01-2009", "30-03-2009", "07-04-2009", "07-04-2009 11:12");
    
    define("MINUTE", 60);
    define("HOUR", 3600); // 60 * 60 
    define("DAY", 86400); //  60 * 60 * 24 
    define("WEEK", 604800); // 60 * 60 * 24 * 7
    define("MONTH", 2592000); // 60 * 60 * 24 * 30
    define("YEAR", 31536000); // 60 * 60 * 24 * 365
    
    function timeSince($date) {
    
        $since = abs(strtotime('now') - strtotime($date));
    
        if($since > YEAR) {
            $year = floor($since / YEAR);
            return "more than $year year(s) ago";
        }    
    
        if ($since > MONTH) {
            $month = floor($since / MONTH);
            return "about $month month(s) ago";
        } 
    
        if ($since > WEEK) {
            $week = floor($since / WEEK);
            $day = floor(($since - ($week * WEEK)) / DAY);
            return "about $week week(s), and $day day(s) ago";
        }
    
        if ($since > DAY) {
            $day = floor($since / DAY);
            $hour = floor(($since - ($day * DAY)) / HOUR);
            return "about $day day(s), $hour hour(s) ago";
        }
    
        if ($since > HOUR) {
            $hour = floor($since / HOUR);
            $minute = floor(($since - ($hour * HOUR)) / MINUTE);
            return "about $hour hour(s), $minute minute(s) ago";
        }
    
        if ($since > MINUTE) {
            $minute = floor($since / MINUTE);
            return "$minute minute(s) ago";
        }
    
        return "under 1 minute ago";	
    
    }
    
    foreach($testcase as $test) {
        echo "Time since $test is " . timeSince($test)."<br/>";
    }   
    
    ?>

Fonte: komunitasweb

Questo post è stato postato sabato, Maggio 9th, 2009 at 11:05 nella categoria Programmazione, Siti Web, tutorial. Tags:, , , , .
Puoi seguire tutti i commenti di questo articolo attraverso RSS 2.0 feed. Puoi lasciare un commento, o trackback dal nostro sito.

Lascia un commento

You must be loggati to post a comment.