views:

65

answers:

3

Hi, I have problem with date function in php. If I supply string in format "d.m.y" for example "01.01.01" it gets rendered as todays date which means that php gets confused.

I found:

Note:

The "Day, month and two digit year, with dots or tabs" format (dd [.\t] mm "." yy) only works for the year values 61 (inclusive) to 99 (inclusive) - outside those years the time format "HH [.:] MM [.:] SS" has precedence.

on: php.net site

How to override this behavior?

I know of date_create_from_format function which would work fine if I knew input will always be in format "d.m.y", but it won't.

UPDATE 1:

Code

$date = new DateTime('01.01.01');
echo $date->format('Y-m-d');

outputs 2010-10-19 and I wanted 2001-01-01.

+1  A: 

To format a date other than now, use the second parameter. For example:

echo date("d.m.y", 1255982665);

echoes 19.10.09

Chris Henry
Doesn't answer the question.
poke
A: 
function getDateFromString( $str )
{
    $date = DateTime::createFromFormat( 'd.m.y', $str );
    if ( $date !== false )
        return $date->getTimestamp();

    // you can try other common formats here
    // ...

    // otherwise just parse whatever there is
    return strtotime( $str );
}

echo date( 'd.m.Y H:i', getDateFromString( '01.01.01' ) ); // 01.01.2001 20:14

Edit

To adjust it a bit more to get your exact output:

function getDateTimeFromString( $str )
{
    $date = DateTime::createFromFormat( 'd.m.y', $str );
    if ( $date !== false )
        return $date;

    // you can try other common formats here
    // ...

    // otherwise just parse whatever there is
    return new DateTime( $str );
}

$date = getDateTimeFromString( '01.01.01' );
echo $date->format( 'Y-m-d' ); // 2001-01-01
poke
Why the downvote?
poke
I don't really like idea of doing testing myself for all possible formats, and I was more hoping to find easy php way. However I like your answer most.
Milan
Well, you don't have to test all possible formats, it's enough to check those that won't work with the normal `new DateTime( $str )`; and that shouldn't be that many different formats.
poke
Only one problem remains, which I just discovered, and that is: On production server I have php 5.2.6 and DateTime::createFromFormat is not supported in that version.
Milan
@Milan: You can use `strptime` instead to parse a given date format and pass its results to `DateTime::setDate` or `mktime` to construct the date.
poke
To the downvoters: Could you please be so kind to leave a reason why you downvote the answer? It's getting quite annoying.
poke
+1  A: 

Just read the documentation! PHP's site is excellent

It seems like you want to reformat a date?

mktime() gives unix timestamp from component pieces date() gives string from unixtimestamp (or implied now) getdate() gives assoc array from unix timestamp

I think you want -

$arr = explode($dateIn, ':'); //get array [day, month, year]
$timestamp = mktime(0,0,0, $arr[0], $arr[1], $arr[2]) //unix time stamp, a long integer representing time
date(DESIREDFORMAT, $timestamp);

check out the output formats here - http://us2.php.net/manual/en/function.date.php

jon_darkstar
To quote OP: *“if I knew input will always be in format "dd.mm.yy", but it won't.”* – So your `explode` won't help.
poke
Poke is right. Your solution does help in case of "." as delimiter. I would first have to find delimiter that is character that is not number and use it as explode parameter.
Milan