Tags
Asked 2 years ago
9 Jun 2021
Views 168
sarah

sarah posted

how to Convert DateTime to String php ?

how to Convert DateTime to String php ?
david

david
answered Apr 26 '23 00:00

To convert a DateTime object to a string in PHP, you can use the format () method of the DateTime class. The format () method takes a string argument which specifies the format you want the date to be in.

Here's an example:



$dateTime = new DateTime('now');
$dateString = $dateTime->format('Y-m-d H:i:s');
echo $dateString;

In the example above, we create a new DateTime object representing the current date and time. Then we use the format () method to format the date as a string in the format Y-m-d H:i:s, which represents the year, month, day, hour, minute, and second.

You can replac e Y-m-d H:i:s with any other format you want, using the format codes specified in the PHP manual for the date() function.
Post Answer