The following PHP code will return a PHP string variable that is the current server date and time in a format like 2021-06-7 19:43:56. Here is a PHP code example of how the PHP date() function works.
1 2 3 |
$current_datetime = date('y-m-d h:i:s'); |
Using PHP echo we can output the value of the above PHP string variable:
1 2 3 4 |
$current_datetime = date('y-m-d h:i:s'); echo "$current_datetime"; |
The above PHP code would display something similar depending on your server date/time:
2021-06-07 10:13:06
Let’s take a closer look at the original PHP code:
1 2 3 |
$current_datetime = date('y-m-d h:i:s'); |
The PHP date() function has different parameters that can be used to output different formats of time and date as outlined as follows:
d: this is the day of the month using two digits ranging from 01 to 31
m: this is the month using two digits randing from 01 to 12
h: this is the hour of the day
i: these are the minutes
s: this represents seconds of a minute
Here is an example of adding hours to date/time using PHP:
1 2 3 |
$datetimecreated = date('y-m-d h:i:s', strtotime('+7 hour')); |