tags:

views:

93

answers:

4

I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.

Here is my code:

date('g:i', strtotime($row['startTime']))

An example of I have the time displayed in my database is like this: 00:12:30

Why is it showing '4:00' every time?

A: 

Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).

Svend
the startTime column is a time datatype
zeckdude
A: 

strtotime converts a date time string to a Unix timestamp.

Perhaps your $row['startTime'] doesn't qualify as a date time string.

None of the examples here discussed a date time string which did not include a date.

The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.

pavium
+2  A: 

strtotime expects a datetime format ... you should do

date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
solomongaby
I did that, but now it shows up as '12:00' every time
zeckdude
check that $row['startTime'] has a value, since it sounds likes its empty
solomongaby
+1  A: 

As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:

 $date_text = $row['startTime']; // assuming the format "00:12:30"
 list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
 /* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
 these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
 */

 echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";
David Thomas
where are the variables $hrs, $mins, and $secs coming from?
zeckdude
I'll update the code to explain.
David Thomas