Home >>Advance PHP Tutorial >PHP Date and Time
PHP Date and Time are the functions that generally permit the user to obtain the date and time from the server where the PHP script runs. The user can use the date/time functions in order to format the date and time in various ways.
Please note that these functions generally are dependent on the locale settings of the user's server. Make sure to take the daylight saving time and leap years into consideration while working with these functions of the PHP language.
Let's understand each of the functions that are included in the date and time functions of the PHP.
The PHP date () function is generally used to format a timestamp towards a efficient readable date and time.
Here is the syntax of the same:date(format,timestamp)
Parameter | Description |
---|---|
format | This is required as this specifies the format of the timestamp |
timestamp | This is basically optional as it specifies a timestamp and the current date and time are known to be the default. |
The way to format a date is generally decided by the required format parameter of the date() function.
Here are some of the characters that are most commonly used for dates that are depicted below:
There are some other characters, like"/", ".", or "-" that can also be inserted between the characters in order to add additional formatting.
Here is an example of the same that has depicted the three format of dates:<?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?>
In order to update the copyright year automatically on any website, use the date () function.
Here is an example of the same:© 2010-<?php echo date("Y");?>
There are various characters that are used to display the time in PHP, some of them are depicted below:
<?php echo "The time is " . date("h:i:sa"); ?>
There are some times when the time the user gets back from the code is generally not correct and it happens because the server might be in another country or the set up is for a different time zone. Hence, in order to get the time to be correct and according to a specific location the user can set the time zone according to their use.
Here is an example that basically sets the timezone to "America/New_York:<?php date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa"); ?>
The optional timestamp parameter that exists in the date() function basically specifies a timestamp. The current date and time will be used in case this is edited from the code.
The PHP mktime() function is known to return the Unix timestamp that is for a date. The number of seconds that exists between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified is generally included in the UNIX timestamp.
Here is the syntax of the same:mktime(hour, minute, second, month, day, year)Here is an example
<?php $date=mktime(11, 14, 54, 5, 30, 2019); echo "Here is Created date " . date("Y-m-d h:i:sa", $date); ?>
The PHP strtotime() function is a function that is generally used in order to convert a human readable date string into a Unix timestamp that is the number of seconds since January 1 1970 00:00:00 GMT.
Syntaxstrtotime(time, now)Example
<?php $date1=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date1) . "<br>"; $date2=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $date2) . "<br>"; $date3=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date3) . "<br>"; ?>