PHP date, time, mysql_fetch problem

Discussion in 'PHP' started by tomjerryhacker, Aug 4, 2009.

  1. #1
    Friends,

    I need to get data from mysql table and display them in which one field is mysql datetime field.

    Now, i want only time from this datetimefield to be displayed while using echo. i have used mysql_fetch() to collect the data from table and list() to assign the data to variables i want. Then i tried to use strtotime and date() function to get the time as output.

    But strtotime() is not returning any value where date() is returning some wierd value like this 01/70/01-00:18:31

    this is my sample query.

    $sql= select * from table1;
    $tmp= mysql_query($sql);

    while($list($abc, $xyz, $ntime)=mysql_fetch($tmp))
    {
    $lmn = strtotime($ntime);
    $pqr = date('H:i:s',$lmn);

    echo "$lmn <br/> $pqr" ;
    }

    Now, the result is,
    <blankspace nothing returned for $lmn > 01/70/01-00:00:00
    <"> 01/70/01-00:00:00
    <"> 01/70/01-00:18:31

    Thanks in advance.

    Regards,
    Urs Tom
     
    tomjerryhacker, Aug 4, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Can you echo out the values in the date column. Are they in the correct datetime format yyyy-mm-dd hh:mm:ss?
     
    jestep, Aug 4, 2009 IP
  3. tomjerryhacker

    tomjerryhacker Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yah when i echo out without using these functions, i get the date in this format

    Example:
    echo "$abc, $xyz, $ntime";

    this is giving me a result like this

    0000-11-11 01:23:24
    1111-11-11 00:00:00

    Is there anything wrong with the values that i uploaded into the table?? will the values change the output??
     
    tomjerryhacker, Aug 4, 2009 IP
  4. flakas123

    flakas123 Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Could you paste one random datetime entry from your mysql?

    Btw, couldn't you just print out the value?
     
    flakas123, Aug 4, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Are the year's actually 0000 and 1111. strtotime, can't go further back than Jan 1 1970.
     
    jestep, Aug 4, 2009 IP
  6. tomjerryhacker

    tomjerryhacker Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    With your suggestion

    I tried to change the default values of yr as month date as 1971-11-11

    Now i get the out put like this

    this is orginal date: 1971-11-11 12:34:45

    using strtotime for this: 58710885

    Using date('H:i:s', strtotime(this date)) : 00:32:51

    everything is different. !!!
     
    tomjerryhacker, Aug 4, 2009 IP
  7. flakas123

    flakas123 Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    As i understand, you want to use just time (hours, minutes, seconds). Why don't you use explode function?
     
    flakas123, Aug 4, 2009 IP
  8. tomjerryhacker

    tomjerryhacker Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I couldnt understand where to use it.
    Can u give me a sample code, where you u want me to use.
     
    tomjerryhacker, Aug 4, 2009 IP