Home > Article > Backend Development > PHP date and time functions_PHP tutorial
1. Time zone setting
Method 1: Set date.timezone=Asia/Hong_Kong in php.ini so that the system default time is East 8th District
Method 2: Use the function date_default_timezone_set() to set the time zone to date_default_timezone_set("Asia/Hong_Kong")
2. Get the current timestamp
Use the time() function to directly return the current time and date. The timestamp represents the number of seconds from 0:00:00 on January 1, 1970 to the running time of this program
3. Get the current date and time
Use the date() function to return the current date. date() has many parameters. If you use "d", it will return the date from 01 to 31 of the current month
4. Parse date and time into Unix timestamp
Use the maketime() function to generate the corresponding Unix timestamp
Let’s look at an example:
date_default_timezone_set("Asia/Hong_Kong");
$timefuture = mktime(0,0,0,01,01,2016);
$timenow = time();
$timecount = $timefuture - $timenow;
$day = round($timecount/86400);
echo date("The current time is: Y year m month d day [l] H point i minute s second",$timenow)."
";
echo "There are ".$day." days until January 1, 2016."."
";
?>
The running result is: