Reputation: 3277
I have a ridiculous string provided by data that isn't adjustable and I need to convert it to a timestamp in a consistent manner.
5162016
sometimes 2 digit month, sometimes leading zeros sometimes not.
I tried using DateTime
$time = \DateTime::createFromFormat("njY", "5162016");
$timestamp = $time->getTimestamp();
I tried both njY
and mdY
but both return a timestamp that converts to 05/01/0020
Any ideas would be appreciated.
Upvotes: 1
Views: 54
Reputation: 36924
Normalize your data, so that the month will be always with a leading zero.
$normalizedDate = str_pad("5162016", 8, 0, STR_PAD_LEFT);
$time = \DateTime::createFromFormat("mdY", $normalizedDate);
$timestamp = $time->getTimestamp();
This only works if the Day of the month is always a 2 digits with leading zeros.
Upvotes: 1