You could try the DateTime
object function createFromFormat
$date = DateTime::createFromFormat('D - M d, Y', 'your date here');
echo $date->format('Ymd');
Question
I have a custom date format the strtotime()
doesn't recognize, and since I'm working with jquery / the user - I want the user to see a specific formatted date so no compromise. Is there a separate method to convert date formats? Currently my format looks like this: D - M d, Y
and I want to convert it over to something like this: Ymd
- is there a different function than strtotime()
?
If it matters I am using WordPress but it didn't seem to have any helpful date formatting functions.
Solution
You could try the DateTime
object function createFromFormat
$date = DateTime::createFromFormat('D - M d, Y', 'your date here');
echo $date->format('Ymd');
OTHER TIPS
You can use DateTime::createFromFormat()
to parse the input into a DateTime
object. Then use DateTime::format()
to convert to the desired output format:
// parse input to a DateTime object
$dateTime = DateTime::createFromFormat('D - M d, Y');
// format to desired output format
echo $dateTime->format('Ymd');