View Full Version : Dealing with really old dates in php

12-16-2003, 03:24 AM

I need to display dates which fall outside of the usable date range. Any ideas?

I'll tell you a bit more: The website has several articles about various people, including the dates on which they were born and died, (if they are in fact dead).

I need to format these dates for display but my testing machine uses windows and therefore the date() function is no use before 1970. I will be using dates as early as the 15- or 1600's so I am assuming that any OS the site is hosted on will not have system support for those dates.

The dates are stored in a MySQL date field yyyy-mm-dd. Does anybody know how I can format these dates somehow getting around the range limits of the date() function?



12-16-2003, 11:07 AM
Have a look at the calendar functions

Maybe this PEAR class supports editing of old dates too

It depends, however, what you actually want to do with the dates. Complex date arithmetic or just retrieving some info like the name of day of birth? The links above should help you get started, though.

12-16-2003, 11:08 AM
You can use some RegExp like this one:

$MySQLDate = "15501122133055"; //just a sample MySQL Date String
$mdy = preg_replace("/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]*)$/", "$2. $3. $1", $MySQLDate);


12-16-2003, 12:40 PM
I don't see the problem.

date() is used to create/format a date, right? So what is your actual problem?

You get the date as a string with a fixed structure (YYYY-MM-DD), and you want to turn it into a string with another structure (MM-DD-YYYY or MM/DD/YYYY or whatever) Has got nothing to do with dates, as far as i understand it
So all you need are a few (1) stringfunctions. Like

$datefromdb = '1568-01-18'
$newdate = substr($datefromdb, -2) . '/' . substr($datefromdb, 5,2) . '/' . substr($datefromdb,0,4) ;

$newdate then contains 18/01/1568

12-16-2003, 05:30 PM
or better still use MySQL date formatting functions to format the dates before they get out of the DB , eg

"SELECT DATE_FORMAT(date , '%W %M %Y' ) as formatted_date FROM $table"

thats assuming your dates are within the MySQL allowed range from the year 1000 , to the year 9999

12-16-2003, 05:59 PM
So far firepages wins! I'm looking for the easiest way to format the date, and working with regular expressions or a bunch of string functions, while they work, are not really that fun.

There is a good chance I will have to do some date arithmatic in the future as well, and the MySQL date functions will take care of that, and all of my dates are within the MySQL range.



12-16-2003, 10:45 PM
mySQL has built in functions to extract months, dates, year, hours, minutes and seconds from a date, time, or datetime field.
here is an example query:

SELECT birthdate
MONTH(birthdate) AS birthmonth, DAYOFMONTH(birthdate) AS birthday
FROM meets

this goes along with what firepages said. personal preference, i guess.