Archive for the ‘PHP’ Category

PHP function: calculate x seconds, minutes or hours ago

Tuesday, February 14th, 2012

Here is a Twitter style PHP time interval calculation function that will return the time difference between two dates in days, hours, minutes or seconds depending on the time interval between the two times.

If the time interval is within 60 seconds, then ‘X seconds ago’ will be displayed. If the interval is between 60 seconds and 60 minutes then ‘X minutes ago’ will appear. Similarly, ‘X hours ago’ will be displayed for a time interval greater than 60 minutes but less than 24 hours and ‘X days ago’ for anything above 24 hours.

The time interval function takes in three arguments:

  1. The old date – the initial date\time in the standard MySQL format i.e. “Y-m-d H:i:s”
  2. The new date – again, in the format “Y-m-d H:i:s”. To use the current ‘now’ time, pass date(“Y-m-d H:i:s”) as the argument
  3. The time interval type. If you want the function to return the most appropriate unit of time automatically, use ‘x’ but this could be overridden by specifying the values for seconds, minutes, hours or days using the values ‘s’, ‘m’, ‘h’ or ‘d’ respectively.

Here is the PHP time interval function:

<?php
function xTimeAgo ($oldTime, $newTime, $timeType) {
        $timeCalc = strtotime($newTime) - strtotime($oldTime);        
        if ($timeType == "x") {
            if ($timeCalc = 60)) {
                $timeType = "m";
            }
            if ($timeCalc = (60*60))) {
                $timeType = "h";
            }
            if ($timeCalc = (60*60*24))) {
                $timeType = "d";
            }
        }        
        if ($timeType == "s") {
            $timeCalc .= " seconds ago";
        }
        if ($timeType == "m") {
            $timeCalc = round($timeCalc/60) . " minutes ago";
        }        
        if ($timeType == "h") {
            $timeCalc = round($timeCalc/60/60) . " hours ago";
        }
        if ($timeType == "d") {
            $timeCalc = round($timeCalc/60/60/24) . " days ago";
        }        
        return $timeCalc;
    }
?>

The following example calls this function, passing 3 arguments for old date (MySQL datetime field from a recordset), new date (current time) and time type (x for automatic).

<?php echo xTimeAgo($row_rsRecordset1['dateField'], date("Y-m-d H:i:s"), "x"); ?>

The resulting time string should appear as ‘X seconds ago’ or ‘X minutes ago’ etc depending on the time interval.

PHP get first paragraph from a string function

Thursday, February 2nd, 2012

Here’s a useful PHP function to return the first paragraph from a HTML text string. It involves finding the character position of the first closing “p” tag from the first paragraph.

Add the following function somewhere in you PHP (I usually have a global functions file).

<?php 
    function getFirstPara($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        return $string;
    }
?>

Then call the function, passing the string variable:

<?php echo getFirstPara($HTMLString; ?>

If you wanted to remove the paragraph tags from the HTML, the function would change to:

<?php 
    function getFirstPara($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        $string = str_replace("<p>", "", str_replace("<p/>", "", $string));
        return $string;
    }
?>

That’s it!

Reset PHP Recordset for Multiple Repeat Regions

Saturday, January 7th, 2012

Occasionally in your PHP application, you may need to have multiple repeat regions on one page using the same recordset. If you don’t reset the recordset after the first time you use a repeat region, the content in the second repeat region will appear blank.

(more…)

Apache php display friendly server error

Saturday, November 26th, 2011

Recently, I’ve started working on a few PHP projects on a local development machine and have been getting frustrating “500 internal server errors”. The machine is a a windows 7 PC running Apache server, PHP and MySQL. (more…)

Scheduling a custom php script URL with plesk

Tuesday, October 25th, 2011

Setting up custom scripts to run automatically has many uses, for example I use scripts for tasks including MySQL database backup and email and managing notifications from website portals. The steps below run through how to set a scheduled task to call a PHP script from your website.

Although I’m running Plesk 9 on a dedicated Windows server, this should apply to other versions of Plesk and server setups: (more…)