0

All I need is to compare dates represented by string values in the format d.m.y (e.g. 15.07.11 for today).

strtotime unfortunatelly gives me wrong results (probably because of the format). I found a answer to my question (PHP strtotime incorrect conversion) but DateTime::CreateFromFormat is not available in my PHP version. I could not find strfptime but think the autor of the answer meant strptime. The result of strptime is a array. I was surprised that I can compare arrays but the compare result is not valid.

What would be the easiest way to compare dates in the given string format with PHP <= 5.1.6?

Community
  • 1
  • 1
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
  • related: http://stackoverflow.com/questions/1841308/php-date-comparison – AJ. Jul 15 '11 at 13:47
  • 1
    Why don't you just convert d.m.y to Y-m-d first and use `strtotime`? – Jürgen Thelen Jul 15 '11 at 13:55
  • @AJ: All answers to your link are using `strtotime` but as I wrote, this function couldn't handle the given format. – Christian Ammer Jul 15 '11 at 13:55
  • 1
    yikes, why are you still using PHP <= 5.1.6??? 5.1 was released six years ago! Support was dropped for it five years ago. Are you really telling me you've been running an unsupported (and hackable) version of PHP for five years? – Spudley Jul 15 '11 at 13:59
  • @Jürgen Thelen: Thank you, I only thougt of PHP functions and didn't see the obvious way! – Christian Ammer Jul 15 '11 at 14:00
  • @Spudley: It's not for me I'm writing the code for a client. PHP <= 5.1.6 is a requirement. – Christian Ammer Jul 15 '11 at 14:03
  • possible duplicate of [php dateTime::createFromFormat in 5.2?](http://stackoverflow.com/questions/5399075/php-datetimecreatefromformat-in-5-2) – Gordon Jul 15 '11 at 14:28
  • and some additional asking exactly the same in http://stackoverflow.com/search?q=[php]+createFromFormat – Gordon Jul 15 '11 at 14:28
  • @Gordon: You are right, I asked a question which already was asked earlier, sorry for that. I like to positivly contribute StackOverflow, what in your opinion should I do now? Delete it, or should the community decide? – Christian Ammer Jul 15 '11 at 14:49
  • @Christian Im not sure you can delete it after it has received an answer, so its probably just best to let it sit and either community will keep closevoting or the closevotes simply wither away after some time. If you want to have it deleted at any cost, flag it for moderator attention. – Gordon Jul 15 '11 at 14:58
  • http://stackoverflow.com/a/17084893/212940 – vascowhite Jun 13 '13 at 11:06

1 Answers1

1

You could always pass the result of strptime to mktime and get a usable Unix timestamp which you can compare or feed to the DateTime objects.

<?php
    $d = strptime('15.07.11', '%d.%m.%y');
    $timestamp = mktime($d['tm_hour'], $d['tm_min'], $d['tm_sec'], $d['tm_mon'], $d['tm_mday'], 1900 + $d['tm_year']);
    echo date("j F Y", $timestamp);
?>

The only thing to watch for is that strptime gives the year as the number of years since 1900, and mk_time just takes a year number, so I added 1900 to it.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145