Laravel
261
w3alert.com
30-01-2020
The date and time functions are most commonly used in PHP. Which are used to convert the date and time format and one format to another format and PHP has some built-in functions to perform these tasks. With the help of which date and time are made understandable by humans.
The computer stores the date and time in a format called UNIX timestamp, which measures time as the number of seconds since the starting of the UNIX era [ Midnight Greenwich Mean Time on January 1, 1970, to January 1, 1970, 00:00: 00 GMT ]
Since this is an abstract format for humans to read, PHP changes timestamp to a format that is readable and more understandable to humans.
The PHP date() function is a pre-defined function that is used to convert date and time formats to human-readable formats.
Syntax:
date( format, timestamp);
There are two parameters used in the PHP date() function.
Format: In the PHP date () function, the format parameter executes the date and time format.
Timestamp: This is an optional parameter if the timestamp is not set then the current date and time will be used.
The format parameter of the PHP date() function is a string in which different characters are used to execute the date in different formats.
The following date related formatting characters are shown here:
Example: Below program explains the usage of the PHP date() function.
Let's create a program to convert date and time format.
<!DOCTYPE html> <html> <body> <?php /* Date formates */ echo date( "d" ) . "<br>"; // the day of the month in two digit. echo date( "D" ) . "<br>"; // the day of th week in text. echo date( "m" ) . "<br>"; // the month in number. echo date( "M" ) . "<br>"; // the month in text. echo date( "y" ) . "<br>"; // the year in two digit. echo date( "Y" ) . "<br>"; // the year in four digit. echo date( "d/m/y" ) . "<br>"; echo date( "d M y" ) . "<br>"; echo date( "d-m-Y" ) . "<br>"; echo date( "d.M.Y.D" ) . "<br>"; ?> </body> </html>
The following characters can be used with date() function to format the time. they are shown here:
Example:
Let's create a program to get time with the date () function of PHP.
<!DOCTYPE html> <html> <body> <?php /*Get Date and Time*/ echo date("d M Y h : i : s a") . "<br>"; echo date("d M Y H : i : s A"); ?> </body> </html>
The time() function is a built-in function in PHP which used to get the current time as a Unix timestamp ( the number of seconds since the starting of the UNIX era: 1 January 1970
Syntax:
time( );
Note: PHP time() function does not support any parameter.
The following time-related formatting characters are shown here:
Example:
Let's create a program to get the current time with the PHP time() function.
<!DOCTYPE html> <html> <body> <?php /*Get time*/ echo date("d M Y H : i : s A", time()); ?> </body> </html>