i
PHP Variables
PHP Data Types
PHP Echo & Print
PHP Strings
PHP Numbers
PHP Constants
PHP Operators
PHP if...else...elseif Statements
Switch Statement
PHP Loops
PHP Arrays
Superglobals
PHP Coding Standards
PHP Form Handling
PHP Form Validation
PHP URLs Validation
PHP Form Required Validation
Complete Form Example
PHP File Functions Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies Handling
PHP Session Handling
PHP filter_var() Function
PHP Validation Filters
PHP Sanitization Filters
Using Filters
Filters Advanced
JSON
PHP Date and Time
MySQL Database
MySQL Connect
MySQL Commands-Creating a Table
MySQL Commands-Inserting The data
MySQL Commands-Prepared Statement
MySQL Commands-Selecting The Data
MySQL Commands-Where and Order By
MySQL Commands-Deleting And Updating The Data
PHP-OOP Introduction
PHP-Classes/Objects
PHP-Constructor/Destructor
PHP-Access Modifiers
PHP-Inheritance
PHP-Inheritance and Protected Access Modifier
PHP-Overriding Inherited Methods
PHP-Final keyword
PHP-Abstract Classes
PHP-Constants
PHP-Traits
PHP-Static Methods and Properties
Introduction to Functions
Defining A function
Returning Values From A Function
Dynamic Function Calls
Variable Scope
Understanding Arguments Or Parameters
Testing For A Function Existence
Returning Multiple Values From A Function
Making practical Use By Building Code Libraries For Code Re-usability
Using Include() And Require()
Date and time functions are very important in any programming language as they allow us to keep track of the time whenever an action is performed. For example, when you create a PHP website where users login and log out. You need to have a proper track of their login date and time. PHP uses date() and time() functions to serve the purpose.
1 Time() and Getdate() function
Many times, you need the information about the current time and PHP time() function helps you to get all the information about current date with time(). Time function doesn’t need any argument and returns an integer when you use time() function. This integer is number of seconds since midnight GMT on 1st January 1970 and returned as time stamp.
For example:
echo time();
?>
It will give output like: 1575192825
It is very difficult to understand the timestamp, but PHP offers some tools to convert this timestamp into a readable form. We can convert the timestamp using getdate() and date() function. We will discuss getdate() in this section and date() function in the next section.
Getdate() accepts an optional timestamp and it returns an associative array that has the information about the date and time.
Some characters that are used for the format parameter of getdate() function are:
seconds – It represents the second past minute from 0-59.
minutes – It represents the minutes past the hour from 0-59.
hours – It represents the hours of the day from 0-23
mday – It represents the day of the month from 1-31.
wday – It represents the day of the week from 0-6.
year – It represents the year in four digits.
yday – It represents the day of the year from 0-365.
weekday – It represents the day of the week in string.
month – It represents the month of the year in string.
mon – It represents the month of the year from 1-12.
For example :
$getdate_array = getdate();
foreach ( $getdate_array as $key => $val )
{
print "$key = $val
";
}
$result_date = "Today's date is: ";
$result_date .= $getdate_array['mday'] . "/";
$result_date .= $getdate_array['mon'] . "/";
$result_date .= $getdate_array['year'];
echo $result_date;
?>
Output of the above php script will be
seconds = 2
minutes = 45
hours = 9
mday = 1
wday = 0
mon = 12
year = 2019
yday = 334
weekday = Sunday
month = December
0 = 1575193502
Today's date is : 1/12/2019
2 Getting date using Date() Function
To format a date or time in PHP, date() function is used and it allows to format a timestamp into a user-friendly way to read date and time.
Syntax for date function is date(format, timestamp) where format specifies the format of the timestamp and timestamp has current date and time by default. Timestamp is an optional parameter which includes a sequence of characters with respect to date and time.
Some characters that are usually used for the format parameter of date function to get the date are:
d – It represents the day of the month from 01 to 31.
m – It represents the month from 01 to 12.
Y – It represents the year in four digits.
l – Lowercase letter ‘L’ represents the day of the week.
You can also insert “/”. “-“, “.” for using additional parameters or multiple parameters for the format.
For example:
echo “ Today’s date is “. date(“Y/m/d”). “
”;
echo “ Today’s date is “. date(“Y-m-d”). “
;
echo “ Today’s date is “. date(“Y.m.d”). “
”;
echo “ Day of the week is” .date(“l”);
?>
Output of the above php code will be:
Today’s date is 2019/12/01
Today’s date is 2019-12-01
Today’s date is 2019.12.01
Day of the week is Sunday
You can also copyright the year for your website using date() function. For example, your website is built in 2018 and you want to copyright it till date then you can write below code in php.
© 2018-
Output of the above php code will be : © 2018-2019
3 Getting Time using date() function
You can also get time using date() function and below are some characters used to get the time information using date() function.
H – It represents the hour in 24-hour format from 00 to 23.
h – It represents the hour in 12-hour format from 01 to 12.
i – It represents the minutes past the hour from 00 to 59.
s – It represents the seconds past the minutes from 00 to 59.
a – It represents the time in am or pm i.e. Ante meridiem and Post meridiem.
For example:
echo “ The current time is “ . date(“h: i: s a”);
?>
Output of the above php script will be like :
The current time is 03:57:06 pm
You can also set the time zone if you need the time to be according to your location. For example, when you want to set the timezone to America or New York then you can write the below script.
date_default_timezone_set(“America/ New_York”);
echo “The time is” .date(“h:i:sa”);
?>
The output will be the current time according to the America and New York time zones.
Don't miss out!