Home >Backend Development >PHP Problem >How to convert php string to time
How to convert php string into time: first create a PHP sample file; then enter the content "$date3=date('Y-m-d H:i:s',"1228348800");"; finally execute this Just file.
Recommended: "PHP Tutorial"
PHP Date and Timestamp Conversion
There are two types of PHP time, one is the timestamp type (1228348800), the other is the normal date format (2008-12-4)
So there are two forms of saving to the database (Really, there are only two types of my applications). I save the timestamp type as a string, which is more convenient.
Normal date type is saved as DATE type.
Please pay attention to these two. I usually use two. Therefore, the table I built a few days ago saved the time type as DATE. I have been saving it with timestamp, but the data cannot be written into the table. I have been debugging it for a long time. Only then did I realize that the error was that the type did not match and was not written to the library.
The timestamp above is more convenient, but when displayed, it is not necessary to show this 1228348800 string to the customer. GOD NOWS!
So the conversion of these two is used. First, let’s talk about how to get the current timestamp. $date1=time(); In this way, the current timestamp is obtained. It needs to be converted back to 2008- For the format 12-4, the date() function is used. The date() function is commonly used in PHP. If you want to get the current date, you can use $date2=date('Y-m-d'); about the parameters inside. What does it mean? If you don’t understand, just check the php manual.
Okay, let’s get down to business. Convert 1228348800 to the 2008-12-4 format code as follows:
$date3=date('Y-m-d H:i:s',"1228348800");
This is OK, such as If you still want to get hours, minutes and seconds, just change 'Y-m-d', but please note that the PHP time still seems to have an error of 8 hours. Add it and it will be OK.
Timestamp Converting to normal date is available. On the contrary, to convert normal date format to timestamp, please see the following code:
$year=((int)substr("2008-12-04",0,4));//取得年份 $month=((int)substr("2008-12-04",5,2));//取得月份 $day=((int)substr("2008-12-04",8,2));//取得几号 echo mktime(0,0,0,$month,$day,$year);
In this way, you can convert normal date to timestamp. If there are minutes and seconds, the same is true here. reason,
The above is the detailed content of How to convert php string to time. For more information, please follow other related articles on the PHP Chinese website!