Converting the string to Date and DateTime uses several functions/methods like strtotime(), getDate(). We will see what these functions do.
strtotime() – This is basically a function which returns the number of seconds passed since Jan 1, 1970, just like a linux machine timestamp. It returns the number of seconds passed according to the parameter passed to the function.
Return Type Returns the number of seconds passed since Jan 1, 1970.
getDate() This function return the date/time information of the passed parameter(date/time);
Parameter The parameter is optional as it takes the current local time as default parameter.
Return Type It returns the information of the date, day, year, month etc in an array.
Code for converting a string to date
Array ( [seconds] => 0 [minutes] => 0 [hours] => 0 [mday] => 21 [wday] => 6 [mon] => 5 [year] => 2011 [yday] => 140 [weekday] => Saturday [month] => May  => 1305936000 )
Code for converting a string to dateTime
Note1 We can use “D” in the place of “d” for getting the day in the output
Note 2 We can use “H” in the place of “h” for getting the time in 24 Hour format in the output
Similarly, the “i” and “s” could also be changed to uppercase to find different outputs, it is possible but not of much use.
- PHP date() format when inserting into datetime in MySQL
- Convert String to Date in Java
- Date after adding given number of days to the given date
- Program for converting hours into minutes and seconds
- PHP | DateTime sub() Function
- PHP | DateTime add() Function
- PHP | DateTime modify() Function
- PHP | DateTime diff() Function
- PHP | DateTime format() Function
- PHP | DateTime createFromFormat() Function
- PHP | DateTime setDate() Function
- HTML | DOM Input Datetime Object
- Converting seconds into days, hours, minutes and seconds
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.