Question

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
Was it helpful?

Solution

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));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top