Date problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Date problem

Post by PhpDog »

I'm grabbing data from a MS SQL 2005 server and using:

$startDate = date("d-m-Y", strtotime($row[2]));

to change the data from the returned array into a date string with format d-m-Y for my purpose.

A data record of 21/12/1989 00:00:00 gives me 21-12-1989 which is fine.
However a data record of 31/12/2039 00:00:00 gives me 01-01-1970 which is anything but fine.

What gives guys?

I'm using PHP version 5.2.5
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: Date problem

Post by SidewinderX »

From http://us3.php.net/manual/en/function.time.php
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.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Re: Date problem

Post by PhpDog »

I see .. thanks for the heads up. However, the last part of the quote "... PHP 5.1.0 and newer versions overcome this limitation though." fell flat for me on 5.2.5.

I guess I'll have to write a cheesy function to overcome this.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Date problem

Post by Ollie Saunders »

I guess I'll have to write a cheesy function to overcome this.
I think you'll have to write a couple of classes to overcome that and have the bcmath extension enabled. You're Better off looking for existing code.
PhpDog
Forum Commoner
Posts: 58
Joined: Mon Jan 14, 2008 10:23 am

Re: Date problem

Post by PhpDog »

This worked for me:

Code: Select all

function formatDate($input)
{
 
  $months[1] = "Jan";
  $months[2] = "Feb";
  $months[3] = "Mar";
  $months[4] = "Apr";
  $months[5] = "May";
  $months[6] = "Jun";
  $months[7] = "Jul";
  $months[8] = "Aug";
  $months[9] = "Sep";
  $months[10] = "Oct";
  $months[11] = "Nov";
  $months[12] = "Dec";
  
  
  $dates = explode(" ", $input);
  $month = array_keys($months, $dates[0]);
  
  // --- if month number < 10 add 0 prefix -------
  if($month[0] < 10)
   {
     $month[0] = "0".strval($month[0]);
   }
   
  // --- if(day number < 10 add 0 prefix ---------
  if($dates[1] < 10)
   {
     $dates[1] = "0".strval($dates[1]);
   }  
   return $dates[1]."-".$month[0]."-".$dates[2];
 
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Date problem

Post by Ollie Saunders »

Ah OK. You aren't doing what I thought you were.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: Date problem

Post by SidewinderX »

PhpDog wrote:I see .. thanks for the heads up. However, the last part of the quote "... PHP 5.1.0 and newer versions overcome this limitation though." fell flat for me on 5.2.5.

I guess I'll have to write a cheesy function to overcome this.
I'm pretty sure they are referring to the latter limitation - not both limitation(s).
Post Reply