Date and time in PHP:
Date and time are some of the most frequently used operations in PHP while executing SQL queries
or designing a website etc.
PHP serves us with predefined functions for these tasks. They are-
1. date() Function:
• The PHP date() function converts timestamp to a more readable date and time format.
• The computer stores dates and times in a format called UNIX Timestamp, which measures time
as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time
on January 1, 1970, i.e. January 1, 1970, 00:00:00 GMT ).
• Since this is an impractical format for humans to read, PHP converts timestamp to a format that
is readable and more understandable to humans.
Date and time in PHP:
• Syntax: date(format, timestamp)
• The format parameter in the date() function specifies the format of returned date and time.
• The timestamp is an optional parameter, if it is not included then the current date and time will
be used.
• Example:
<?php
echo "Today's date is :";
$today = date("d/m/Y"); Output:
Today's date is :27/01/2025
echo $today;
?>
Date and time in PHP:
• Formatting options available in date() function:
The format parameter of the date() function is a string that can contain multiple characters
allowing to generate the dates in various formats. Date-related formatting characters that are
commonly used in the format string:
1. d: Represents day of the month; two digits with leading zeros (01 or 31).
2. D: Represents day of the week in the text as an abbreviation (Mon to Sun).
3. m: Represents month in numbers with leading zeros (01 or 12).
4. M: Represents month in text, abbreviated (Jan to Dec).
5. y: Represents year in two digits (08 or 14).
6. Y: Represents year in four digits (2008 or 2014).
Date and time in PHP:
• The parts of the date can be separated by inserting other characters, like hyphens (-), dots (.),
slashes (/), or spaces to add additional visual formatting.
• Example:
<?php Output:
Date formats:
echo "Date formats:" . "<br>\n"; 27/01/2025
27-01-2025
echo date("d/m/Y") . "<br>\n"; 27.01.2025
27.Jan.2025/Mon
echo date("d-m-Y") . "<br>\n";
echo date("d.m.Y") . "<br>\n";
echo date("d.M.Y/D");
?>
Date and time in PHP:
2. time() Function:
The time() function is used to get the current time as a Unix timestamp (the number of
seconds since the beginning of the Unix epoch: January 1, 1970, 00:00:00 GMT).
The following characters can be used to format the time string:
1. h: Represents hour in 12-hour format with leading zeros (01 to 12).
2. H: Represents hour in 24-hour format with leading zeros (00 to 23).
3. i: Represents minutes with leading zeros (00 to 59).
4. s: Represents seconds with leading zeros (00 to 59).
5. a: Represents lowercase antemeridian and post meridian (am or pm).
6. A: Represents uppercase antemeridian and post meridian (AM or PM).
Date and time in PHP:
Example of time() function:
<?php
$timestamp = time();
echo($timestamp);
echo "\n";
echo(date("F d, Y h:i:s A", $timestamp));
?>
Output: 1737997908
January 27, 2025 06:11:48 PM
Date and time in PHP:
3. mktime() Function:
• The mktime() function is used to create the timestamp for a specific date and time. If no date
and time are provided, the timestamp for the current date and time is returned.
• Syntax:
mktime(hour, minute, second, month, day, year)
• Example:
<?php
echo mktime(20, 21, 50, 01, 27, 2025); Output:
1738005710
?>
Code Reusability:
• Code Reusability is a practice of writing software code in a way that allows it to be reused in
different parts of the same program or in different projects without having to be rewritten
from scratch.
• Code Reusability is fundamental to efficient software development, as it reduces redundancy,
promotes maintainability, and speeds up the development process.
• Development teams can write software components with the vision to reuse them in the
future, or, on some occasions, developers may realize that they have code from previous
projects that can be reused by sheer coincidence.
• In PHP, code reusability can be achieved using include and require statements.
PHP include and require Statements:
• PHP allows us to create various elements and functions, which are used several times in
many pages. It takes much time to script these functions in multiple pages.
• Therefore, use the concept of file inclusion that helps to include files in various programs and
saves the effort of writing code multiple times.
• PHP allows you to include file so that a page content can be reused many times. It is very
helpful to include files when we want to apply the same HTML or PHP code to multiple pages
of a website.
• There are two ways to include file in PHP.
1. include
2. require
PHP include and require Statements:
1. include
• This function is used to copy all the contents of a file called within the function text wise into
a file from which it is called. This happens before the server executes the code.
• It is nothing but calling a file into another file. Whenever you have a requirement to called
same piece of code which is available inside another page into required file than we have to
go with include mechanism.
• Syntax: There are two ways to use include:
include 'filename ';
Or
PHP include and require Statements:
Example;
File1.php <?php
echo "Hello Everyone";
?>
File2.php <?php
include(“File1.php");
Output:
echo "<br>Good Morning" Hello Everyone
Good Morning
?>
PHP include and require Statements:
2. require() function:
• The require() function performs same as the include() function.
• It also takes the file that is required and copies the whole code into the file from where the
require() function is called.
• Syntax:
require(“filename”);
Example: <?php
require(“File1.php");
echo "<br>Above File is Required"
?>
PHP include and require Statements
• Now, if we don’t have a file named File1.php, then in the case of the include(), the output will
be shown with warnings about a missing file, but at least the output will be shown from the
File2.php file.
• In the case of the require(), if the file PHP file is missing, a fatal error will rise and no output is
shown and the execution halts.
• Thus require() function is better than the include() function since the script should not
continue executing if files are missing or such an error is generated.
PHP include and require Statements
• include_once() & require_once():
The include_once() and require_once() statements will only include the file once even if asked
to include it a second time i.e. if the specified file has already been included in a previous
statement, the file is not included again.
Example:
Example: x.php
<?php
echo “Hello Everyone";
?>
PHP include and require Statements
<?php
include_once('header.inc.php');
include_once('header.inc.php');
?>
Output: Hello Everyone
<?php
require_once('header.inc.php');
require_once('header.inc.php’)
Output: Hello Everyone
PHP Include Paths:
• The PHP Include Path is a set of locations that is used for finding resources referenced by
include/require statements.
• Elements added to a project's Include Path affect the following:
1. Running/Debugging
- Files that are called with a 'relative path' will be searched for during runtime in the resources
and order specified in the include path.
2. Go to source
- Files that are called with a 'relative path' will be searched for according to the resources and
order specified in the project's include path.
PHP Include Paths:
• In 'include'/'require' calls, file locations can be defined in three ways:
1. Absolute Path- The exact file location is specified (e.g. C:\Documents and Settings\
MyProject\myfolder\a.php). During PHP Web Page Running/Debugging the Path Mapping
mechanism will be activated.
2. Relative to the Current Working Directory - File names preceded with a "./" or a "../" These
will only be searched for relative to the PHP 'Current Working Directory'. You can find out
the location of your Current Working Directory by running the command "echo getcwd()".
3. Relative Path - Only the file name or partial path is specified (e.g. /myfolder/a.php). In this
case, PDT will search for the file's path according to the resources and order configured in
the project's Include Path.
PHP Include Paths:
PHP set_include_path() Function:
• The set_include_path() function is an inbuilt function in PHP that sets the include_path
configuration option.
• Syntax:
string|false set_include_path(string $include_path)
• Parameters: This function accepts one parameter that is described below:
o $include_path: This parameter specifies the new value for the include_path.
• Return Value: This function returns the old include_path on successful, otherwise returns
“false”.
PHP Include Paths:
Example:
<?php
$path = "/home/myfolder" ;
if(true == set_include_path($path)){
echo "Path is set now" ;
} else {
echo "Path is not set now" ;
?>
PHP Include Paths:
PHP get_include_path() Function:
• The get_include_path() function is an inbuilt function in PHP that returns the current
include_path to the caller.
• Syntax:
string|false get_include_path()
• Parameter: This function does not accept any parameters.
• Return Value: This function returns the path in the string format, or “false” otherwise.
PHP Include Paths:
<?php
$path1 = get_include_path() ;
$path2 = ini_get('include_path') ;
if($path1 == $path2){
echo "Both the paths are same." ;
} else {
echo "Both the paths are not same." ;
?>