Organizational Research By

Surprising Reserch Topic

using strtotime for dates before 1970

using strtotime for dates before 1970  using -'php,mysql'

I have a text column in mysql and it stores a date value in the format yyyy-mm-dd.
Now, in my php page, I use this code to parse into a date value.

date("F j, Y", strtotime($row['value']));

Now, I just read that strtotime() parses values only after January 1, 1970. I have lot of date values before that date. Is there a work around? I don't want to change my database structure.

asked Oct 7, 2015 by vickeykumar66
0 votes

Related Hot Questions

3 Answers

0 votes

From the documentation for strtotime():

strtotime() has a range limit between Fri, 13 Dec 1901 20:45:54 GMT and Tue, 19 Jan 2038 03:14:07 GMT; although prior to PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some operating systems (Windows).

What version of PHP are you running? And on what platform? Perhaps it's time for an upgrade.

If you're working with dates outside the 13 Dec 1901 to 19 Jan 2038 range, then consider using PHP's DateTime objects which can work with a much wider range of dates.


$date = date_create($row['value']);
if (!$date) {
    $e = date_get_last_errors();
    foreach ($e['errors'] as $error) {
        echo "$error\n";

echo date_format($date, "F j, Y");


try {
    $date = new DateTime($row['value']);
} catch (Exception $e) {
    echo $e->getMessage();

echo $date->format("F j, Y");
answered Oct 7, 2015 by param.oncemore
0 votes

I'm a noob, and was having some similar issues regarding the use of strtotime vs explode. I ran across this thread and was greatly helped by the comments on the change in date limit by Mark Baker (thanks!). So I wrote this code just to play with the concept. Perhaps it will help other noobs such as myself.

Just change the date on the top PHP line and see what happens to the dates below - very interesting. Thanks again!

answered Oct 7, 2015 by akhilesh
0 votes
function safe_strtotime($string)
    if(!preg_match("/\d{4}/", $string, $match)) return null; //year must be in YYYY form
    $year = intval($match[0]);//converting the year to integer
    if($year >= 1970) return date("Y-m-d", strtotime($string));//the year is after 1970 - no problems even for Windows
    if(stristr(PHP_OS, "WIN") && !stristr(PHP_OS, "DARWIN")) //OS seems to be Windows, not Unix nor Mac
        $diff = 1975 - $year;//calculating the difference between 1975 and the year
        $new_year = $year + $diff;//year + diff = new_year will be for sure > 1970
        $new_date = date("Y-m-d", strtotime(str_replace($year, $new_year, $string)));//replacing the year with the new_year, try strtotime, rendering the date
        return str_replace($new_year, $year, $new_date);//returning the date with the correct year
    return date("Y-m-d", strtotime($string));//do normal strtotime
answered Oct 7, 2015 by virendra.bajaj