jabber
answered Apr 26 '23 00:00
You can use the strtotime () and date() functions to convert a string to a date in PHP. The
strtotime
() function will convert a string to a Unix timestamp, and the date() function can format that Unix timestamp into a desired date format.
Herephp's an example code snippet:
$date_str = '2022-05-01'; // your date string
$date = strtotime($date_str); // convert to Unix timestamp
$formatted_date = date('Y-m-d', $date); // format the date as desired
echo $formatted_date; // output: 2022-05-01
In this example, the $ date_str variable holds the date string that you want to convert. The strtotime () function converts the string to a Unix timestamp, which is stored in the $date variable. Finally, the date() function formats the Unix timestamp as a string in the desired format, which is then stored in the $ formatted_date variable.
You can change the format of the output date by changing the argument passed to the date () function. For example, date('d/m/Y', $date ) will format the date as dd/mm/yyyy.