Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to work with historic dates (as in 10,000 or a million years ago) in PHP?

I'm parsing Wikidata JSON datasets to collect historic data. So far I haven't found the right format to store them in PHP / MySQL (via Doctrine).

For the last few millennia, DateTime seems to work, but I don't want to limit my application to that. It might be totally possible that it will have to process the start time property of the Universe. Besides, I also want to store the precision of the data, since we might know the rough year of birth of one person and the exact minute for another one. (Edit: For now, dates are enough, I can live without the time, my example was exaggerated. Still, I sometimes know the exact date, sometimes just the month or even the year.)

I've thought about creating my own class for dates (I'm not planning to calculate time differences or something like that), but I also don't want to re-invent the wheel.

Search results on a certain search engine as well as here are ... underwhelming, sadly.

Any ideas or experience that you can share?

like image 291
Christian Avatar asked Apr 04 '19 20:04

Christian


1 Answers

For a kind of similar use-case, we ended up with storing values for each period of time, allowing to store everything in the db and query at various precision levels.

class YourAwesomeDateManager {

    public
        $millenium,
        $century,
        $decade,
        $year,
        $month;
        // and so on...

    function setDate() {
        // Hydrate with the best precision available
    }

    public function getDate ($precision) {
        // ...
    }
}

This allows you to retrieve different date formats, and return 0 for unavailable precision fields

The thing is that there is no common name usage for periods above milleniums. So you'll have to be creative.

like image 71
Creaforge Avatar answered Nov 05 '22 21:11

Creaforge