PDA

View Full Version : Handling dates older that 1970 through PHP


redsox9
03-21-2006, 08:31 AM
Hello, everybody!

As I continue to upgrade my site, I will have a section that will include memorable moments and some of those date back before 1970. Now I've found out firsthand that PHP versions less than 5.1 running through Unix have a hard time recognizing these dates.

I believe the question has been asked but does BlueHost plan to upgrade PHP to the most recent version or is it too buggy at this point? Supposedly it will handle dates as early as 1900.

Also, I realize that this becomes a problem when a database stores information like birthdays, e.g. a patient's age who was born prior to 1970. I have a rather long work-around where I simple break out the year, month, and day into separate fields but obviously this is slightly inconvenient. Has anyone else who has run into this problem developed a simple solution?

My guess is that there may be others with this issue so your suggestions would be invaluable.

jjn
03-21-2006, 04:45 PM
Below is a excerpt from the PHP manual (Date function), you should not have any trouble with the dates until 2038 on an Unix machine. Windows has the date limitation from 1/1970 through 2038.

PHP Manual
Prev Next

--------------------------------------------------------------------------------

date
(PHP 3, PHP 4 , PHP 5)

date -- Format a local time/date
Description
string date ( string format [, int timestamp])


Returns a string formatted according to the given format string using the given integer timestamp or the current local time if no timestamp is given. In otherwords, timestamp is optional and defaults to the value of time().

Note: The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). On Windows this range is limited from 01-01-1970 to 19-01-2038.

Note: To generate a timestamp from a string representation of the date, you may be able to use strtotime(). Additionally, some databases have functions to convert their date formats into timestamps (such as MySQL's UNIX_TIMESTAMP function).

Hope this helps,
Jim

redsox9
03-21-2006, 05:30 PM
Hi, Jim:

So, does this mean that the browser is responsible for the dates displaying as they do? I'm slightly confused...

FYI, this was the usual function that I used to call the date:

$thedate = date("j F Y",strtotime($row['dob']));

Now that I think about, I just noticed that some of the players on the Sox born before 1970 (Wells, Schilling, Timlin, Wakefield - what is it about 40-something pitchers on this team?) have birthdates listed as 30 November 1999.

Well, isn't that special?! :rolleyes:

Basil
03-23-2006, 04:05 PM
I doubt the date issue even applies to what you're trying to do. How are you storing these player's information that PHP would be required to calculate their birthday? Please provide an actual example.

redsox9
03-24-2006, 05:34 AM
Here's the deal:

I have players' birthdates stored in a MySQL database using the DATE context (yyyy-mm-dd). When I retrieve these date from the database, they get saved in a string as such:

$thedate = date("j F Y",strtotime($row['dob']));

Now, some player's birthdates are before 1970, eg. Curt Schilling b. 1966-11-17. When my script outputs the date to the browser (FireFox), it reads "31 December 1969."

FYI, I haven't looked into the UNIX_TIMESTAMP function yet as suggested by Jim so I'll look into that next.

Bottom line: I understand that there are limits but where does the translation get bumped? I hope that I'm making sense here...

Early Out
03-24-2006, 06:42 AM
The problem must be browser-specific. When I go to your website, and pull up the profile on Schilling, his DOB is shown as "14 November 1966."

redsox9
03-24-2006, 06:49 AM
The problem must be browser-specific. When I go to your website, and pull up the profile on Schilling, his DOB is shown as "14 November 1966."

Ah, except that you probably looked at my current site

http://www.fenwayfanatics.com/redsox/2006_team/players_schilling.html

which is simple HTML. Here's where his page would be on the new site:

http://www.fenwayfanatics.com/redsox/player.php?player_id=1021

His DOB here is called out from the MySQL database.

Early Out
03-24-2006, 07:04 AM
Ah, right you are - I didn't take the time to view/source. Yup, on the new site, Schilling gets a few years younger, and turns into a New Year's Eve baby.

So much for easy answers!

lazynitwit
03-24-2006, 03:47 PM
The browser has no effect in this at all, the date is being generated on the server end, not the client end.
As for your problem about dates pre-1970, I am not having this problem on Bluehost. I used the following code:
<?php
echo date("j F Y", strtotime("1941-12-07"));
?>
and it outputted 7 December 1941 as expected. As a note, I tested this on two servers, box31 and box32.

redsox9
03-24-2006, 04:27 PM
Hi, lazynitwit:

I believe that I'm on box12 - I forget how to check - and still no luck.

Interestingly, I tried this (arrows add for effect):

$query = "SELECT * FROM roster r WHERE r.player_id = 1021";
$result = mysql_query($query,$dbh) or die(mysql_error());
$row = mysql_fetch_array($result);

$p_name = $row['first']." ".$row['last'];
$p_dob = $row['dob'];

print "$p_dob<br />";
print "$p_name<br />";

print date("j F Y", strtotime($p_dob))."<br />"; <--
print date("j F Y", strtotime("1941-12-07"))."<br />"; <--


Here's the output:

1966-11-14
Curt Schilling
31 December 1969 <--
31 December 1969 <--


So there is SOMETHING happening when I try to use the strtotime function where the original, correct date is getting lost in the translation.

Scarecrow: This is no time to cry for help! Help! Help! :rolleyes:

lazynitwit
03-24-2006, 04:47 PM
Being that your box is not working properly with these dates, you could try this for a solution. The ADOdb Date Time Library (http://phplens.com/phpeverywhere/adodb_date_library) is a rewrite of PHP's getdate(), date(), gmdate(), mktime(), gmmktime(), and such. Unfortunately, I do not think strtotime() works with it (you may try it, but I am unsure as to it actually working). A workaround to that would be to include the library to your PHP file and use something like:
list($year, $month, $day) = explode("-", $row['dob']);
print adodb_date("j F Y", adodb_mktime(0, 0, 0, $month, $day, $year));


This might not be a very good solution, but unless Bluehost resolves the issue on your box it should work.

redsox9
03-24-2006, 05:11 PM
That looks like it might be the solution! Being that I'm still learning PHP, is there an easy way to call these functions without an include file? I'm sure that I could add them to the root .htaccess file but is there another solution?

In the meantime, I'm going to put in a ticket and see if BlueHost can solve this for me. Thanks again! :)

lazynitwit
03-24-2006, 05:23 PM
You can use auto_prepend_file in php.ini (or php_auto_prepend in .htaccess if I remember correctly) to automatically include a file to the top of PHP scripts. You can find it about halfway down the page on http://php.net/ini.core

Edit: I had a mistake for .htaccess (I think it is right now)

redsox9
03-24-2006, 06:46 PM
Hello, again:

I'm trying to call out the file with the auto_prepend_file command and I added this line to my php.ini file:


auto_prepend_file = ".:/home/fenwayfa/public_html/includes/adodb-time.inc.php"


However, that doesn't seem to work. I've tried six ways to Sunday to get it to recognize this "library" but no dice. ???

redsox9
03-24-2006, 07:14 PM
Okay, actually I realized that I needed to grab the entire adodb library so now I am calling


auto_prepend_file = ".:/home/fenwayfa/public_html/includes/adodb/adodb.inc.php"


Still, no dice. Ugh! :o I have to get that ticket into BlueHost...

lazynitwit
03-24-2006, 07:48 PM
Okay, actually I realized that I needed to grab the entire adodb library so now I am calling


auto_prepend_file = ".:/home/fenwayfa/public_html/includes/adodb/adodb.inc.php"


Still, no dice. Ugh! :o I have to get that ticket into BlueHost...

Actually you do not need the entire adodb database, the time functions work stand-alone. You should also just try putting one file in, so it would be more like
auto_prepend_file = "/home/fenwayfa/public_html/includes/adodb-time.inc.php"