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

You can format a date in the desired format using the DateTime class in PHP. Here's an example code snippet to format the date:

main.php
// Set the date time string
$dateString = '2022-08-26 18:00:00';

// Create a new DateTime object
$date = new DateTime($dateString, new DateTimeZone('UTC'));

// Set the timezone to your desired timezone
$date->setTimezone(new DateTimeZone('America/Los_Angeles'));

// Format the date string into the desired format
$dateFormatted = $date->format('F j, Y g:i a');

// Output the formatted date string
echo $dateFormatted; // Output: August 26, 2022 11:00 am
458 chars
15 lines

In this example, we first create a DateTime object with the input date string and set the timezone to UTC. We then convert the timezone to the desired timezone (America/Los Angeles) using the setTimezone() method. Finally, we format the date string into the desired format using the format() method with the F j, Y g:i a format string.

The F format code stands for the full month name, j stands for the day of the month without leading zeros, Y stands for the four-digit year, g stands for the hour in 12-hour format without leading zeros, i stands for the minutes with leading zeros, and a stands for the lowercase am/pm marker.

gistlibby LogSnag