format a date in to the format month day, year hour:minute:second am/pm in php

To format a date in the given format, you can use the date() and strtotime() functions in PHP.

Here's an example code snippet:

main.php
$date_string = '2022-12-31 18:30:00';
$date_time = strtotime($date_string);
$formatted_date = date('F j, Y g:i:s a', $date_time);

echo $formatted_date; // Output: December 31, 2022 6:30:00 pm
193 chars
6 lines

Explanation:

  1. First, we create a string representation of our date and time.
  2. Second, we use the strtotime() function to convert this date string to a Unix timestamp.
  3. Finally, we pass this timestamp and the desired date format to the date() function. The format string F j, Y g:i:s a represents the required format of month, day, year, hour, minute, second, and am/pm.

This will output the formatted date as December 31, 2022 6:30:00 pm.

gistlibby LogSnag