How to Convert a PHP String to Date
When working with programming languages, it is common to need to convert an item from one data type to another. There is often a need to convert something a user has provided into a usable type---such as converting from a user string into a date. In php, all dates are stored as UNIX timestamps. Fortunately, in php 4 and php 5, there is a pre-defined function that will allow you to convert nearly any string into a UNIX timestamp.
Instructions
-
-
1
Obtain the string you want to convert into a date. This can be through a text box on an HTML form or it might be from a database table.
-
2
Store this string in a string variable using the assignment statement. For example, if using a form, $myDate = $_POST["txtDate"];
-
-
3
Define a variable to store the resulting date. Do this using the assignment operator and store a zero in the variable: $resultDate = 0;
-
4
Use the strtotime function to convert the string and store it in the date variable: $resultDate = strtotime($myDate);
-
1
Tips & Warnings
Use the php date function to display the date in nearly any format.