2

Im making Biogram on my site which lists known historical figures.

Right now, Im just getting values from database, and on many IF statements, determine what to display.

 /* @var $bio \Models\Library\Biography */
 $birth = [];
 $death = [];
 $date = null;

 if($bio->getBirthMonth() != null) {
    if ($bio->getBirthDay() != null) {
       $birth = [
           $bio->getBirthDay(),
           $bio->getBirthMonth(),
           $bio->getBirthYear(),
       ];
    } else {
       $birth = [
           $bio->getBirthMonth(),
           $bio->getBirthYear(),
       ];
    }
 } else {
    $birth = [
        $bio->getBirthYear(),
    ];
 }

 if($bio->getDeathMonth() != null) {
    if ($bio->getDeathDay() != null) {
       $death = [
           $bio->getDeathDay(),
           $bio->getDeathMonth(),
           $bio->getDeathYear(),
       ];
    } else {
       $death = [
           $bio->getDeathMonth(),
           $bio->getDeathYear(),
       ];
    }
} else {
    $death = [
       $bio->getDeathYear(),
    ];
}

if (!array_filter($birth) && array_filter($death)) {
    $date = 'zm. ' . implode('.', $death);
}
if (array_filter($birth) && !array_filter($death)) {
    $date = 'ur. ' . implode('.', $birth);
}
if (!array_filter($birth) && !array_filter($death)) {
    $date = null;
}
if (array_filter($birth) && array_filter($death)) {
    $date = implode('.', $birth) . ' - ' . implode('.', $death);
}

But first of all, Im not happy with this kind of code (don't know if I can write it better).

Secondly when im using Carbon (for example), and want to display year from medival ages, it looks like 0552 instead of 552.

The last thing is that there is no "year 0" in history. There were year 1 After Christ and 1 Before Christ. So when I want to have year -200 it gives me -199.

I know this may be handled by adding -1 to date if it's BC, but I think this is not the right way.

Is there any php library for DateTime that handles all the Dates well?

Also can I, and if so; how to rewrite this code above to look better?

Cheers :)

Thyrun
  • 101
  • 1
  • 10

1 Answers1

2

This is a very interesting problem.

Firstly your if statements - I would write a helper function within $bio that contains the logic regarding what to display as your date, making reference to the other properties that exist within $bio. You could call it getPrettyDate() or something like that. That's potentially where your logic for getting the year to display correctly would go too.

After some considerable googling and not much luck, this answer seems to be the most helpful on this subject, certainly some food for thought.

It's also worth noting that using DATE when persisting this information to your database (assuming you're using MySQL, which you may well not be) is probably not the way forward in your case - as noted in that answer, the MySQL definition of DATE is the following

DATE

A date. The supported range is '1000-01-01' to '9999-12-31'.

I have also after writing this just stumbled across this answer which might be the best approach. Good luck!

More reading on ISO-8601 in the context of 'Year 0' here.

Community
  • 1
  • 1
d0ug7a5
  • 692
  • 4
  • 7