Creating new Date Time from string

In PHP, you can use the DateTime class to create a new date and time object from a string. The class has a constructor that takes a string argument in a format that represents a date and time.

Here is an example of how you can create a new DateTime object from a string:

<?php

$date = new DateTime('2022-11-15 12:30:00');

You can also specify the format of the string using DateTime::createFromFormat method.

<?php

// Create a DateTime object from a formatted string
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2022-11-15 12:30:00');

// Format the DateTime object as a string
echo $date->format('Y-m-d H:i:s');

// Output: 2022-11-15 12:30:00

?>

You can then use the various methods of the DateTime class to manipulate the date and time object, such as modify, add, and sub.

You can also format the date and time using date_format method.

<?php

// Create a new DateTime object using a string
$date = new DateTime('2022-11-15 12:30:00');

// Format the DateTime object as a string
echo $date->format('Y-m-d H:i:s');

// Output: 2022-11-15 12:30:00

?>

It will print '2022-11-15 12:30:00'