iptracker
answered Apr 26 '23 00:00
To convert a date format in PHP, you can use the date () function, which formats a timestamp as a string. The date () function accepts two parameters: the format of the output string and the timestamp to format. Here is an example of how to use the date () function to convert a date format in PHP:
// Original date string
$date_str = '2022-05-01';
// Convert date format
$date = date('F j, Y', strtotime($date_str));
// Output: May 1, 2022
echo $date;
In this example, the strtotime() function is used to convert the original date string to a Unix timestamp, which can be used as the second parameter of the date () function. The first parameter of the date () function specifies the format of the output string using various format characters. In this case, F represents the full month name, j represents the day of the month without leading zeros, and Y represents the year with four digits.