tags:

views:

61

answers:

3

Hallo can someone explain the behaviour of strtotime function with year in non-standard format.

echo date("d-m-Y",strtotime('02-12-10')) .'<br>'; //10-12-2002
echo date("d-m-Y",strtotime('09.09.10')) .'<br>'; //02-09-2010 --How this is interpreted?


echo date("d-m-Y",strtotime('02-12-2010')) .'<br>'; //02-02-2010
echo date("d-m-Y",strtotime('09.09.2010')) .'<br>'; //09-09-2010

I wanted to convert strings of format dd.mm.yy(09.09.10) to datetime format.

+1  A: 

This is not 2-digit year question. this is non-standard format question.
And asking impossible things from mere a program.
Even me, not being a computer, have no idea what these 09's dooes mean in your date. is it day.month.year? or year.month.day or whatever?
Assuming it's day.month.year:

$list($d,$m,$y) = explode("09.09.10");
echo "$d-$m-20$y";
Col. Shrapnel
That is standard German Format. http://www.sql-server-helper.com/tips/date-formats.aspx . Please check under standard German. Here 09.09.10. Represents 9th September 2010. Moreover i am not asking impossible things from program.
lucky
@lucky well it's local mssql standard. But there are many other standards, and nobody can tell you for sure which one meant here. Actually, strtotime() designed to recognize un-parseable verbal date formats. And it's overkill to use it for such a formal format. I'd use a code I supplied and went on instead of looking for days for justice
Col. Shrapnel
+2  A: 

From the manual:

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.

You are using '09.09.10' and 10 does not fall in the valid range hence change the separator to -

codaddict
Thanku for ur reply and for the explanation. As i am new to PHP it would be great if you can explain me why,echo date("d-m-Y",strtotime('02-12-10')) results in 10-12-2002 instead of 02-12-2010
lucky
@lucky When `strtotime` is ambigous (= when all members have only two digits), it will revert to Y-m-d. I don't think there's anything you can do about that. You need to use a parsing function that you can force a schema on, or manually explode the date like shown by Col. Shrapnel.
Pekka
+4  A: 

strtotime() can be a bit flaky in such cases. It is built to recognize standard american date formats.

If you can use PHP > 5.3, consider using DateCreateFromFormat which has the big advantage of accepting a pre-defined format string to parse the incoming data.

On pre-5.3 platforms, strptime() seems to offer a second-best alternative. It's not available on Windows and has some minor issues - be sure to read the manual page before using.

Pekka
I disagree with this answer on architectural grounds. Non-standard dates should almost always be standardized first, **before** trying to parse them into more usable forms, and if at all possible, validated as correct by the submitter.
hopeseekr
@hopeseekr define "standard". In my native Germany, "09.09.10" is a valid format meaning September 9, 2010. What speaks against converting this - coming e.g. from a user input - into a timestamp *directly*, instead of using cumbersome string operations to make it a ISO date first? Makes no sense to me.
Pekka
@Pekka, even the PHP manual has a great big warning about strptime(). it says to use http://us.php.net/manual/en/function.date-parse-from-format.php instead.
hopeseekr
@hopeseekr look closely at my answer, it's the first thing I recommend, too. It's just not always an option, because it's PHP > 5.3
Pekka
@Lucky get yourself a calendar and look very hard at the "February" page. Notice anything? :D
Pekka
@Pekka, Ich bin verrückt. Vielen dank:)
lucky