Thursday, December 14

Php String And Date Basics

Google+ Pinterest LinkedIn Tumblr +

Basics of PHP – Part 16

This is part 16 of my series, Basics of PHP. In this part of the series, we look at basics of PHP string and date.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at That is, contact me for the slightest problem you have about what you are reading.

A string is a series of characters. A string literal can be specified in 4 different ways, but we shall look only at two: single quoted and double quoted. The argument in the echo construct below is a string literal in single quotes:

    echo ‘His name is John.’;

The argument below is a string literal in double quotes:

    echo “His name is $hisVar”;

With double quotes, the value of a variable in the string literal will appear, in place of the variable, when the string is displayed. So if we had,

    $hisVar = “Peter”;

then the above string will be displayed as, “His name is Peter”. With single quotes, the value of a variable in the string literal does not replace the variable. With single quotes, the variable name including the $ sign is displayed and not the value.

Let us now look at some string functions.

The strlen Function
The strlen function returns the length of a string as number of characters, including spaces. The syntax is:

    int strlen (string $string)

The following statement displays 14.

    echo strlen(“I am a string.”);

Of course, you can assign the return value of the function to a variable, and then use the variable for some other manipulation.

The strpos Function
Character position counting in a string begins from zero. Character position of a character in a string is also called the index position. The strpos function returns the position of the first occurrence of a sub string. In simple terms, the syntax is:

    int strpos(mainString, subString)

The following statement displays, 3:

    echo strpos(“We are dancing.”, “are”);

The substr Function
Remember, character position counting in a string begins from zero. The substr function returns a sub string whose start position is given. The syntax is:

    string substr ( string $string , int $start [, int $length ] )

If the optional length parameter is omitted, the sub string from the start position to the end is returned.

The following statement displays, “are”.

    echo substr(“They are dancing.”, 5, 3);

The main string is “They are dancing.” The start position is 5. The length (number of characters) is 3. So “are” is displayed.

There are many string functions. I have given you just three. The string functions are predefined in PHP. You can consult some other document for the other functions.

There are many predefined functions in PHP you can use to handle dates. In this article, I will only show you how to obtain the current date and time. You use the predefined date function. This function obtains the current date and time of the computer that is running the PHP program. Since PHP is normally run at the server, you can use this function to obtain the current date and time of the server computer and send to the client browser. In simple terms, the syntax is:

    string date ( string $format )

The function returns a string whose content is the current date and time. The parameter, the function takes is also a string. Try the following code, first before we look at the explanation:


        $myDate = date(“m-d-Y,H:i:s”);
        echo $myDate;


In the first statement, the right operand reads the current date and time and assigns the value in string form to the variable, $myDate. The second statement echoes the date and time in string form. The value displayed will be something like:


Let us now look at the argument of the date function. The argument (parameter) is called the String Format, for the current date and time. m in the argument, stands for the current month. The hyphen in the argument is displayed. d stands for current day of the month. The hyphen after d is displayed. Y stands for the current year in four digits. The comma after Y is displayed. H stands for the current 24-Hour. The colon after H is displayed. i stands for minutes. The colon after i is displayed. s stands for seconds. The letters, m, d, Y, H, I, and s must be typed in the same cases as given. You should compare the argument of the date function with the example display given.

Let us stop here and continue in the next part of the series.


To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):

Getting started with PHP
PHP Basic Syntax
Basics of PHP Variables
PHP Conditional Statements
Boolean Logic for PHP
Boolean Logic and PHP Conditions
PHP Comparison and Arithmetic Operators
PHP Loop Statements
PHP Function Basics
PHP Array
Some PHP Predefined Functions and Arrays
PHP Variable Scope Basics
PHP Object Basics
PHP Reference
PHP Error Basics
PHP String and Date Basics
PHP Form Simple Validation with embedded Error Messages from Server


About Author

Leave A Reply