Learning PHP

They say “The more languages you know, the easier it is to learn another one”. I’d say the same is true for programming, in this case, scripting languages.

In my 15+ years of doing Test Automation, it just happened so that I’ve developed most of my scripts with VBScript Scripting language. So it’s about time I learn a new one. For me, the best way to learn something new is by plunging headfirst and learning along the way, solving practical problems as they come. So PHP scripting language was my language of choice when I’ve decided to develop a simple web application that I needed as a demo application for Test Automation.

PHP is a fun language, to say the least! At first, it seems very redundant. Where there is only one way to do something in VB script, there will be at least three different ways for that in PHP. After playing with PHP for some time I’ve developed lots of respect for its versatility (rather than redundancy). By no means, I consider myself an expert after playing with it for a couple of weeks but I would definitely agree with the quote above about learning new languages.

Here is the collection of useful code snippets I’ve found and modified or written myself while learning PHP and developing Guestbook Demo Web Application.

PHP Random String

Useful for generating a unique ID, example below is for 9 character length ID. To modify change the length parameter (now it’s set for -9) for the functionsubstr.

<?php
//generate random string of 9 characters
echo $rnds = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"),-9);
?>

PHP Displaying the relative time difference between two dates. Long and Short versions.

Useful for displaying the time elapsed since the date in the past. For example, time interval since a comment was posted. There are 2 functions, first function – time_elapsed(), displays the long difference between 2 dates (years, months, days, hours, seconds). I got this function here. I wrote the second function – elapsed_short() to display a shorter version, the first non zero time interval only.

For example, if a comment was posted 0 years, 0 months, 0 days, 1 hour, 52 minutes, 5 seconds ago it would just display 1 hour ago

<?php
function time_elapsed($date1, $date2) {
    
    //format dates as such
    $dt1 = new DateTime($date1);
    $dt2 = new DateTime($date2);
    $diff = $dt1->diff($dt2);
    
    $ret = $diff->format('%y years, %m months, %a days, %h hours, %i minutes, %s seconds');
    
    $ret = str_replace(
        array('1 years','1 months','1 days','1 hours','1 minutes','1 seconds'), 
        array('1 year','1 month','1 day','1 hour','1 minute','1 second'), 
        $ret
    );
    return $ret;
}
//------------------------------------------------------
function elapsed_short($val) {
    
    $arr_date = explode(", ", $val);
    $rows  = sizeof($arr_date);
    
    $ret="Just now";

    for ($row = 0; $row < $rows; $row++) { 
            $arr_intrvl = explode(" ",$arr_date[$row]); 
            
            if ($arr_intrvl[0] > 0) {
            $ret = "$arr_date[$row] ago";
            break;
        }
    }
    return $ret;
}
//----------------------------------------------

$date1 = "January 25, 2018, 9:11:25 am";
$date2 = "January 25, 2018, 11:03:30 am";
$datediff = time_elapsed($date1,$date2);
echo "Date 1: $date1<br />";
echo "Date 2: $date2<br />";
echo "Long Difference: $datediff<br />";
$diffshort = elapsed_short($datediff);
echo "Short Difference: $diffshort<br />";
//-----------------------------------------------
?>

Did you find this useful? Leave your comment below.

Categories PHP

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.