tags:

views:

118

answers:

3

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.

+1  A: 

you should use a date columnn, not text one.
and date_format() SQL function to format date in the query

Col. Shrapnel
I can't change the column type. I have several values in a text column and some of these are dates (in the format yyyy-mm-dd). There is also another column which specifies if the row contains a date value or a text value.
Ctroy
In that case, you need to redesign your database.
Jacco
Are you saying there is no work around to this problem, without redesigning the database? I just want to convert a text of format yyyy-mm-dd to date format F j, Y.Eg: 1820-05-20 to May 20, 1820.
Ctroy
there is no problem at all for the date. And the solution I had written. Bot for the whole your database, you already ruined it, if you have data of different kind in one column. There is no solution and it will lead you straight to disaster
Col. Shrapnel
A: 

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.

Procedural:

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

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

OOP

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

echo $date->format("F j, Y");
Mark Baker
A: 

You could format the date using the mysql date_format the date. This site will help you format the date http://www.mysqlformatdate.com

Gerard