MoonPoint Support Logo

 


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
August
Sun Mon Tue Wed Thu Fri Sat
   
24 25 26
27 28 29 30 31    
2017
Months
AugSep
Oct Nov Dec


Fri, Aug 04, 2017 10:51 pm

Calculating working days in PHP

I have an SQLite database that I use to track work requests. Some requests are time sensitive, i.e. they need to be completed within a certain number of days from the time they are approved. So I have an "Approved" and an "Implemented" column in the database to record the date I approved a request and the date it was implemented. All requests should be completed within 5 business days, so when I display the data on a webpage with PHP, I want to see the number of elapsed working days between the time I approved a request and the time it was implemented. To do so, I use some code provided by George John at Calculate business days with a slight modification. The code I use appears below:

function getWorkingDays($startDate, $endDate)
{
    $begin = strtotime($startDate);
    $end   = strtotime($endDate);
    if ($begin > $end) {

        return 0;
    } else {
        $no_days  = 0;
        while ($begin <= $end) {
            $what_day = date("N", $begin);
            if (!in_array($what_day, [6,7]) ) // 6 and 7 are weekend
                $no_days++;
            $begin += 86400; // +1 day
        };

        return $no_days - 1;
    }
}

[ More Info ]

[/languages/php] permanent link

Mon, Jul 03, 2017 10:03 pm

is_null versus empty in PHP

I have a PHP script that queries an SQLite database displaying the results on a web page. There are two fields, "EC Link" and "Configs Link" in the database that can contain URLs pointing to documentation on another server, but sometimes there is no URL in the database for those fields. If there is a URL, I want to display a clickable "EC" and "Configs" links. If there is no URL in either field, I want to simply display the text, but without it being clickable to indicate that there is no URL in the database table. I was using "is_null" for that purpose as shown below:

   if (is_null($row['EC Link'])) {
       echo "EC | ";
    }
    else {
       echo "<a href=\"" . $row['EC Link'] . "\">EC</a> | ";
    }
    if (is_null($row['Configs Link'])) {
       echo "Configs | ";
    }
    else {
       echo "<a href=\"" . $row['Configs Link'] . "\">Configs</a> | ";
    }

But I found that sometimes I was seeing clickable links even though there was no URL for the EC and/or Configs documentation for a particular record in the database. When I checked the records in the relevant table in the database, I found that the fields were blank, i.e., there was no data in them, but they weren't marked as null. So, to allow for the values being either null or blank, I used an empty, instead of is_null, test.

[ More Info ]

[/languages/php] permanent link

Mon, Sep 26, 2016 10:51 pm

Displaying MySQL records with PHP

To query a MariaDB - MariaDB is a fork of MySQL - database using PHP, code similar to that shown below can be used. In this exmple, the account used to query the database is johndoe with a password of ThePassword. The database is named Acme and contains a table named Accounts.

<?php

$con = mysql_connect("localhost","johndoe","ThePassword");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Acme", $con);

$accounts = mysql_query("SELECT * FROM Accounts");

?>

[ More Info ]

[/languages/php] permanent link

Sun, Jun 05, 2016 11:02 pm

Verifying PHP code from the command line

If you need to verify the PHP code in a .php file, you can do so from a command line interface (CLI), e.g., a shell prompt using the -l option; that's the letter "l", not the number "1". E.g., when I tried accessing a web page I had created, e.g., http://www.example.com/sompepage.php, I saw only a blank page. If I examined the source code for the page in the browser from which I was viewing the page, there was nothing there. Looking through the PHP code, the cause of the error wasn't immediately obvious to me, but when I issued the command php -l somepage.php on the server where the page resided, the line that was causing the problem was identified.
$ php -l somepage.php
PHP Parse error:  syntax error, unexpected '$download_url_description' (T_VARIAB
LE) in somepage.php on line 79
Errors parsing somepage.php

Examining the code, I realized I had omitted a required semicolon from the prior line. When I added the semicolon at the end of the line and reran the check, I no longer saw any error messages and when I refreshed the webpage in the browser after making the update, it then displayed correctly.

[/languages/php] permanent link

Sat, Mar 12, 2016 5:24 pm

Determining the username under which PHP is running

I wanted to determine the user name under which PHP was running for a WordPress blog on a hosting site. To do so, I placed a PHP script, whoami_here.php, in the home folder for the WordPress installation and then accessed the webpage for that script via a browser, e.g. http://example.com/whoami_here.php.

There are a variety of methods you can use to check on which account PHP is running under, e.g., <?php passthru("whoami"); ?>, though some methods may not work on some systems.

[ More Info ]

[/languages/php] permanent link

Tue, Dec 30, 2014 1:24 am

PHP script displaying wrong time

I include the following PHP code in web pages on this system to display the last time that a file was updated.
<?php

$thisfile = pathinfo($_SERVER['PHP_SELF']);

echo "Last modified: ".date("l F j, Y g:i A",
filemtime($thisfile["basename"]));

?>
After I replaced the hard drive recently and installed CentOS 7 as a fresh install, the times displayed have been ahead 5 hours, i.e., the PHP code was displaying a time stamp matching Universal Coordinated Time (UTC), also commonly referred to as Greenwich Mean Time, rather than Eastern Standard Time (EST)

I verified the time zone was set correctly at the operating system level with the timedatectl command, so I realized the issue must be with PHP itself. I checked the location of the PHP configuration file, php.ini and found it was located at /etc/php.ini.

$ locate php.ini
/etc/php.ini
/usr/share/doc/php-common-5.4.16/php.ini-development
/usr/share/doc/php-common-5.4.16/php.ini-production

I logged into the root account and checked the contents of the /etc/php.ini file. I found that the timezone was not set in the file:

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

So I removed the semicolon which was making the date.timezone line into a comment and set the time zone to be the appropriate one for Eastern Time (ET).

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone =America/New_York

I then restarted the Apache web server software with the apachectl restart command, which fixed the problem.

[/languages/php] permanent link

Sat, Jul 28, 2012 10:26 pm

Printing Boolean Values as Yes or No

If you have a PHP variable that is storing Boolean data, i.e. 0 for "false" or "no" and 1 for "true" or "yes", but want to display the value as "no" when a zero is stored in the variable and "yes" when a one is stored in the variable, you can print the value using the ternary operator, ?, which is described in the Ternary Operator section of the manual page Comparison Operators.

E.g., suppose I have an array named swinfo that has information on various software packages that has an array variable Free that has a 0 stored in it if the software is not free and a 1 stored in it if the software is free. If I have a software package that isn't free, if I just print the contents of the variable as in the first instance below, I get a zero, but by using ther ternary operator, i.e., the ?, I can specify that I want "yes" or "no" displayed, instead as for the second instance where it is displayed below.

      echo "<table>\n" .

      "<tr>\n" .
       "<td><b>Free:</b></td><td>" .
        $swinfo['Free'] . "</td>" .
       "</tr>\n" .

       "<tr>\n" .
       "<td><b>Free:</b></td><td>" .
        ($swinfo['Free'] ? 'yes' : 'no') . "</td>" .
       "</tr>\n" .

       "</table>\n";

For an instance where the software isn't free, I would see the following displayed:

Free:0
Free:no

References:

  1. Echo boolean field as yes/no or other values
    Stack Overflow
  2. Comparison Operators
    PHP: Hypertext Preprocessor

[/languages/php] permanent link

Sat, Jul 28, 2012 9:12 pm

Printing an array with print_r

The contents of an array can be printed in PHP using print_r. E.g., suppose that the information to be displayed is stored in a database in a table called tests that has student test scores with each student identified by a student id. The following PHP code could be used to fetch the test information for a student whose id is stored in the variable $num and then print the entire array named testinfo.

$result1 = mysql_query("SELECT * FROM tests WHERE ID = $num");
$testinfo = mysql_fetch_array($result1);
print_r ($testinfo);

References:

  1. Print_r () PHP Function
    By Angela Bradley
    About.com PHP/MySQL

[/languages/php] permanent link

Sat, Jan 07, 2012 9:54 pm

Displaying Data from a MySQL Database with PHP

If you have data stored in a MySQL database that you wish to include on a webpage, PHP provides a mechansim to query a MySQL database to extract and dispaly the data on a webpage.

[ More Info ]

[/languages/php] permanent link

Sun, Feb 13, 2011 10:34 pm

Using suexec to run PHP under a different account

I wanted to run PHP for some virtual hosts on a webserver using the user's account for her websites that were handled in Apache's httpd.conf via virtual hosts. To do so, I used use suEXEC, but it took me quite awhile to get it to work..

[ More Info ]

[/languages/php] permanent link

Once You Know, You Newegg AliExpress by Alibaba.com

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo