|  | Posted by NC on 12/09/05 01:28 
Trekmp wrote:>
 > I currently work in ASP and am in the process of moving some
 > application across to PHP.  At present I store dates in a database
 > in numerical format 38694, which is 08/12/2005.  In ASP its very
 > easy to convert this value back into a date, but I cannot find a way
 > to do this in PHP.
 
 First of all, you should have ways of converting the numerical
 presentation into a human-readable date on the database level.  In SQL
 Server, you can use the CONVERT() function for that.
 
 If for whatever reason this is not feasible (although I can't imagine
 why), you must remember that PHP relies on Unix system libraries to
 work with dates.  So PHP is not well-suited to handling dates preceding
 January 1, 1970.  Assuming all your dates are after January 1, 1970,
 here is a quick fix:
 
 function MStoUnix ($MSdate) {
 return round(($MSdata - 25569) * 24 * 60 * 60);
 }
 
 This function converts a Microsoft "timestamp" (number of days since
 January 1, 1900) into a Unix timestamp (number of seconds since January
 1, 1970).  Then you can format the resulting timestamp using the date()
 function.  For example, this snippet:
 
 echo date('Y-m-d', MStoUnix(38694));
 
 would output "2005-12-08".
 
 Cheers,
 NC
  Navigation: [Reply to this message] |