Jump to content


Photo

Formatting time


  • Please log in to reply
3 replies to this topic

#1 kevinmears

kevinmears
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 10 June 2006 - 08:11 PM

Can someone help please. The web page gets a stored time from a table called bookings, the time is stored in the time data type in a sql table. The time is displayed on the web page using:

$row1['time'];

which comes up in the format: hh:mm:ss. However, I just want to display the hours and minutes with no seconds. Can someone help me out,

Thanks

Kevin



#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 10 June 2006 - 08:56 PM

You can check this topic:

[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=95404\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=95404[/a]

MySQL can format the date using DATE_FORMAT
~ D Kuang

#3 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 10 June 2006 - 08:56 PM

You say your date is stored in an sql table. I assume you mean a 'mysql' table. There is a problem with storage formats differences between php and mysql. When you retrieve a date/time value from mysql, first you will have to convert it using:

function convert_timestamp ($timestamp){
    $parts = sscanf($timestamp, '%04u%02u%02u%02u%02u%02u');
    $string = vsprintf('%04u-%02u-%02u %02u:%02u:%02u', $parts);
    return strtotime($string);
}

-then you want to use it like this:
$time = convert_timestamp($row1['time']);

--then you use the php date funtion
echo date("format-string", $time);

[a href=\"http://us3.php.net/manual/en/function.date.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.date.php[/a]

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 10 June 2006 - 09:01 PM

If you want to convert form a MySQL timestamp to UNIX you can use MySQL's built-in UNIX_TIMESTAMP or PHP's strtotime()

[a href=\"http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/5.0/en/dat...-functions.html[/a]
[a href=\"http://www.php.net/strtotime\" target=\"_blank\"]http://www.php.net/strtotime[/a]
~ D Kuang




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users