1

I have date witch is formatted d-m-y like 22-10-49 I want to convert it to Date object to save it in my mysql Database. I try :

DateTime::createFromFormat('d-m-y', $mydate , new DateTimeZone('GMT'));

but the result is 2049-10-22 instead of 1949-10-22. I searched and got that createFromFormat only returns date after 1970. but I don't know what to do.

P.s: 22-10-49 is what I have, and there are several other dates like this, I cannot change the format of it or convert it to 22-10-1949 or any other formats. P.S 2 : "I'm working with birthdays and excpect 22-10-15 to be 1915 rather than 2015.

Shirin Abdolahi
  • 1,047
  • 8
  • 18
  • @Strawberry I did not get it,what do you mean? – Shirin Abdolahi Nov 07 '15 at 09:34
  • @Strawberry 2015-10-22. nothing before 1970 works,date is assumed to be in the range 1970-2069 – Shirin Abdolahi Nov 07 '15 at 09:44
  • Say you had a value like 22-10-15. Would you expect that to materialise as 1915 or 2015? If the answer to that is 'I would expect it to be 2015' then which year is the cut-off? Is it simply that any value greater than '15' refers to a year in the 20th century – Strawberry Nov 07 '15 at 09:50
  • actually I'm working with birthdays, so I expect it to be 1915 cause no one in my database id born in 2015 yet.what's the solution ? @Strawberry – Shirin Abdolahi Nov 07 '15 at 09:52
  • 1
    Now edit your question with the important insights which you have just revealed. – Strawberry Nov 07 '15 at 09:59

1 Answers1

2

Try this function.

Edit: First we will convert two digit year in 4 digit. Then we will form complete date and pass it to function.

 $original_date = '22-10-49';
    $date_part = explode('-',$original_date);

    $baseyear = 1900; // range is 1900-2062
    $shortyear = $date_part[2];
    $year = 100 + $baseyear + ($shortyear - $baseyear) % 100;
    $subdate = substr( $original_date, 0, strrpos( $original_date, '-' ) );
    $string = $subdate."-".$year;

    echo safe_strtotime($string);

function safe_strtotime($string)
{
    if(!preg_match("/\d{4}/", $string, $match)) return null; //year must be in YYYY form
    $year = intval($match[0]);//converting the year to integer
    if($year >= 1970) return date("Y-m-d", strtotime($string));//the year is after 1970 - no problems even for Windows
    if(stristr(PHP_OS, "WIN") && !stristr(PHP_OS, "DARWIN")) //OS seems to be Windows, not Unix nor Mac
    {
        $diff = 1975 - $year;//calculating the difference between 1975 and the year
        $new_year = $year + $diff;//year + diff = new_year will be for sure > 1970
        $new_date = date("Y-m-d", strtotime(str_replace($year, $new_year, $string)));//replacing the year with the new_year, try strtotime, rendering the date
        return str_replace($new_year, $year, $new_date);//returning the date with the correct year
    }
    return date("Y-m-d", strtotime($string));//do normal strtotime
}

Output: 1949-10-22

Source: Using strtotime for dates before 1970

Community
  • 1
  • 1
Suyog
  • 2,472
  • 1
  • 14
  • 27
  • I actually read this date from somewhere else and I have it as `22-10-49`, I sure there should be another way. I don't want to go through `if...else`es to change my dates format. but thank you for your answer – Shirin Abdolahi Nov 07 '15 at 09:24
  • @ShirinAbdolahi, is the date format fix for every date you are getting? i.e. 22-10-49, 22-10-15, 15-07-54 etc. – Suyog Nov 07 '15 at 10:08
  • thank you,it does the trick, but first part is unnessasary, I just added '19' to $date_part[2] , I think there is no better solution.thank you – Shirin Abdolahi Nov 07 '15 at 10:38