views:

79

answers:

1

I am trying to write a function to format a date and time for me. I have an almost identical function which formats just a date. That function works fine. I just added some code to try and have it format the date with a time. It should return something like "May 18, 2009 9:50 PM" but I am getting this warning:

Warning: mktime() expects parameter 6 to be long, string given in
public_html/include/functions.php on line 421

Here is the code I have:

function dateTimeFormat($dateIn)
{
   $x = explode(" ",$dateIn);
   $y = explode("-",$x[0]);
   $z = explode(":",$x[1]);

   $year = $y[0]; 
   $month = $y[1];
   $day = $y[2];
   $hour = $z[0];
   $min = $z[1];

   $dateOut =date("F j, Y h:i A", mktime($hour, $min, 0, $month, $day, $year)); 

   return $dateOut;
}

What it is putting out is wrong too. It puts out:

December 31, 1969 07:00 PM

but the timestamp in the database is

2009-05-18 05:07:39
+4  A: 

PHP already has a perfectly good date-parsing function: strtotime(). It returns a Unix timestamp which you can pass to date().

In other words, your function can be reduced to this:

function dateTimeFormat($dateIn)
{
    return date("F j, Y h:i A", strtotime($dateIn));
}
htw
That got rid of the warning, but it still is displaying wrong. It is still showing: December 31, 1969 07:00 PM but the timestamp in MySQL is 2009-05-18 00:00:00.
Josh Curren
I fixed the problem
Josh Curren
Um, how? Please share!
Jrgns
It was most likely a specific (and probably unrelated) problem with what he was passing to dateTimeFormat(). The code I gave will work as-is.
htw
You might also note that strtotime has a serious memory leak in PHP 5.2.8.
Jeff Ober