Question

I use strftime to format time from a mysql datetime col over a multilanguage website. My goal ist to get this, like a "decimal time":

6 p.m. to 7.30 p.m. or
2.15 p.m. to 5 a.m.

and not sg. like that:

06:00pm to 07:30pm
02:15pm to 05:00am

Maybe someone can help me out!

I tried with this (My experimental sourcecode):

// $var comes in datetime format from mysql
function formatDateTime($format,$var) {
return strftime("$format",strtotime($var));
// Formats: http://php.net/manual/de/function.strftime.php
}

// mysql fetch: ...
echo formatDateTime('%I:%M%P',$row['dateTimeStart']) ... to ...
echo formatDateTime('%I:%M%P',$row['dateTimeEnd'])
Was it helpful?

Solution

I believe this should do what you want:

function formatDateTime($date = 'now', $format = '%l:%M %P') {
    $ret = strftime($format, strtotime($date));
    return str_replace(array('am', 'pm', ':00'), array('a.m','p.m', ''), $ret);
}

Example usage:

echo formatDateTime('1385914593');
echo formatDateTime('08:00:04 AM');
echo formatDateTime('13:26:04');

Output:

5:30 a.m 
8 a.m 
1:26 p.m
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top