-2

I want to parse the date 1938 1938+02:00 using date() & strtotime().

My code:

echo date("Y", strtotime("1938+02:00"));

gives me as result "2014"..

What am i doing wrong?

John Conde
  • 217,595
  • 99
  • 455
  • 496
R2D2
  • 743
  • 1
  • 7
  • 14

3 Answers3

5

For something like this just get the first four characters of the string. No need to work with dates and such:

echo substr('1938+02:00', 0, 4);

Demo

But if you insist on using date functionality you'll need to use DateTime::createFromFormat() as that date string is not a standard format.

$date = DateTime::createFromFormat('YP', '1938+02:00');
echo $date->format('Y');

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

date("Y"); only return the year. That's what the Y does.

See the Manual page for date for other options.

EDIT

Another thing to consider, is that timestamps only go back to 1970. That's what a timestamp is (the number of seconds since 1970).

So, that's going to give you a negative value for the timestamp.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
-1

Your date string is not in an acceptable format. here is a list of acceptable formats for strtotime

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • 1
    This is correct, but you should also show how to work with that date format. Otherwise this answer isn't very helpful. – John Conde Oct 31 '14 at 14:42