Search results
13 maj 2012 · You can use the format method of the DateTime class: $date = new DateTime('2000-01-01'); $result = $date->format('Y-m-d H:i:s'); If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case: if ($result) { echo $result; } else { // format failed echo "Unknown Time"; }
Returns associative array with detailed info about given date/time. The returned array has keys for year, month, day, hour, minute, second, fraction, and is_localtime. If is_localtime is present then zone_type indicates the type of timezone.
2 lut 2024 · How to Convert DateTime to String in PHP. Converting DateTime to string in PHP is quite easy, there are useful functions available that are meant for this purpose like format() of DateTime class, date_format(), using the predefined formats or using list().
Using the format Method. The first method we recommend to use for converting the DateTime object to a string is format. Here is the code to run: <?php $date = new DateTime ('2000-01-01'); $result = $date -> format ('Y-m-d H:i:s'); ?> In case format doesn’t succeed, it returns FALSE.
16 sty 2023 · This article demonstrates how to convert a DateTime object to a string in PHP. You can use the format() function of the DateTime class to convert a DateTime object to string in PHP. The DateTime::format function returns the date formatted according to the given format.
date_parse() parses the given datetime string according to the same rules as strtotime() and DateTimeImmutable::__construct().
You might simply use concat and join them into a string: $arr = array( "1" => "2019", "2" => "5", "3" => "7", "4" => "0", ); $datetime_format = $arr["1"] . "-" . $arr["2"] . "-" . $arr["3"]; var_dump($datetime_format); Output string(8) "2019-5-7" If you wish to have a 4-2-2 format, this might work: