Tags
Asked 3 years ago
9 Jun 2021
Views 270
angeo

angeo posted

How to parse a date string in php ?

How to parse a date string in php ?
debugger

debugger
answered Apr 26 '23 00:00

To parse a date string in PHP, you can use the DateTime class, which provides methods to create and manipulate date objects.

Here's an example:



$dateString = '2022-05-01 14:30:00';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $dateString);

echo $date->format('Y-m-d H:i:s'); // Output: 2022-05-01 14:30:00
In the example above, we first define the date string that we want to parse. We then create a new DateTime object using the createFromFormat method, which takes two arguments: the format of the date string, and the date string itself. In this case, the format is 'Y-m-d H:i:s ', which corresponds to a date string in the format o f YYYY-MM-DD HH:MM:SS. Finally, we use the format method to output the parsed date in the same format as the original string.

Note that the createFromFormat method returns a DateTime object, which allows you to perform various date and time operations on the parsed date.
Post Answer