Tags
Asked 2 years ago
9 Jun 2021
Views 199
angeo

angeo posted

how to Create new Date Time from string in php ?

how to Create new Date Time from string in php ?
jagdish

jagdish
answered Apr 26 '23 00:00

To create a new DateTime object from a string in PHP, you can use the DateTime ::createFromFormat method. This method creates a DateTime object from a string according to a specified format.

Here is an example:

$

dateString = "2022-05-01 12:30:00";
$dateFormat = "Y-m-d H:i:s";

$dateTime = DateTime::createFromFormat($dateFormat, $dateString);

echo $dateTime->format('Y-m-d H:i:s');

In this example, the $dateString variable contains the date and time as a string in the format Y-m-d H:i:s, and the $ dateFormat variable specifies the format of the string. The createFromFormat method creates a new DateTime object from the string using the specified format.

The format method is used to output the date and time in a specific format. In this example, it outputs the date and time in the same format as the input string.

You can change the $ dateString and $ dateFormat variables to match your own input string and format.
Post Answer