You can use DateTime::createFromFormat()
(as other answers already mentioned). However, you have to pay attention to some minor details.
Because you parse a string that contains a partial date (only the month and the year are specified), the rest of the fields (day of month, hour, minute, second) are set, by default, using the current time. Most probably, this is not what you want. I guess you want to get the start of the month (day 1, at midnight).
You can get this by putting an exclamation mark (!
) in front of the format string. It resets all the date components to the Unix epoch (1970-01-01 00:00:00 UTC).
Also, the third parameter of DateTime::createFromFormat()
is the timezone to use. If you don't pass it, PHP uses the default time zone set in php.ini
or by the last call to date_default_timezone_set()
(if there was any). This may or may not be what you need.
$followUpDate = 'M12-16';
// It seems like your timezone is US/Eastern (GMT+5 during the winter)
$timezone = new DateTimeZone('US/Eastern');
// Create a DateTime out of the provided string
// The "!" character in front of the format resets all the fields
// to the Unix epoch (1970-01-01 00:00:00 UTC) before parsing the string
$date = DateTime::createFromFormat('!\Mm-y', $followUpdate, $timezone);
// Display it; it displays 1480568400
echo($date->format("U"));