datetime - PHP - Handling dates before and after christ -


im making biogram on site lists known historical figures.

right now, im getting values database, , on many if statements, determine 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 kind of code (don't know if can write better).

secondly when im using carbon (for example), , want display year medival ages, looks 0552 instead of 552.

the last thing there no "year 0" in history. there year 1 after christ , 1 before christ. when want have year -200 gives me -199.

i know may handled adding -1 date if it's bc, think not right way.

is there php library datetime handles dates well?

also can i, , if so; how rewrite code above better?

cheers :)

this interesting problem.

firstly if statements - write helper function within $bio contains logic regarding display date, making reference other properties exist within $bio. call getprettydate() or that. that's potentially logic getting year display correctly go too.

after considerable googling , not luck, this answer seems helpful on subject, food thought.

it's worth noting using date when persisting information database (assuming you're using mysql, may not be) not way forward in case - noted in answer, mysql definition of date following

date

a date. supported range '1000-01-01' '9999-12-31'.

i have after writing stumbled across this answer might best approach. luck!

more reading on iso-8601 in context of 'year 0' here.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -