format a date in to the format 'yyyy-mm-dd hh:mm:ss' in php

You can use the DateTime class in PHP to format a date. Here's an example:

main.php
$date = new DateTime();
$formatted_date = $date->format('Y-m-d H:i:s');

echo $formatted_date;
95 chars
5 lines

In this example, we create a new DateTime object, which defaults to the current date and time. We then use the format method to format the date in the desired format, which is 'yyyy-mm-dd hh:mm:ss'. The format string is passed as an argument to the format method.

The H format character is used for 24-hour time format, while the h format character is used for 12-hour time format. The i, s, d, m, and Y format characters are used for minutes, seconds, day, month, and year respectively.

The output of the example code will be something like this:

main.php
2021-06-10 11:45:23
20 chars
2 lines

gistlibby LogSnag