Date formatting issues

I am trying to write a function to format date and time for me. I have a nearly identical function that only formats the date. This function works fine. I just added some code to try and format the date with time. It should return something like "May 18, 2009 09: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 he gives out is also wrong. It issues:

December 31, 1969 07:00 PM

      

but the timestamp is in the database

2009-05-18 05:07:39

      

+1


a source to share


1 answer


PHP already has an excellent function of parsing dates: strtotime()

. It returns a Unix timestamp that you can pass to date()

.

In other words, your function can be boiled down to this:



function dateTimeFormat($dateIn)
{
    return date("F j, Y h:i A", strtotime($dateIn));
}

      

+4


a source







All Articles