PHP is a programming language widely used on the Internet. It provides many date and time processing functions. In PHP, we often need to process time into timestamp format. The timestamp is a number that represents the number of seconds from 0:00:00 on January 1, 1970 to the present.
In PHP, there is a very powerful function that can get the current time. This function is called "time()". This function returns the timestamp of the current time.
For example, we can use the following code to get the timestamp of the current time:
$timestamp = time(); echo $timestamp;
This code will output a number representing the timestamp of the current time. If when you run this code, the time is 23:59:59 on December 31, 2021, then the number output by this code is 1640985599.
However, sometimes we need to get a timestamp of a specified date. PHP provides us with another function to implement this function, this function is called "strtotime()".
The "strtotime()" function accepts a string as a parameter and attempts to parse this string into a date and time. If the parsing is successful, it returns a timestamp of this date and time. If parsing fails, it returns false.
For example, we can use the following code to get the timestamp of 0:00:00 on January 1, 2022:
$timestamp = strtotime("2022-01-01 00:00:00"); echo $timestamp;
This code will output a number representing January 1, 2022 Timestamp at 0:00:00 on the day. If when you run this code, the time is 23:59:59 on December 31, 2021, then the number output by this code is 1640995200.
In addition to the "time()" function and "strtotime()" function, PHP also provides many other date and time processing functions. For example, the "date()" function can format the timestamp into a specified Date and time format, you can also use the "mktime()" function to get a timestamp for any date and time.
In short, PHP provides many convenient date and time processing functions, which allow us to easily process time into timestamp format. Whether in developing websites or other applications, timestamps are a very important concept. We need to be proficient in PHP's date and time processing functions to better complete programming tasks.
The above is the detailed content of How to get time in php and change it to timestamp. For more information, please follow other related articles on the PHP Chinese website!