Page 1 of 1

algorithm to display month from timestamp doesnt work???

Posted: Thu Nov 13, 2008 2:13 am
by ThuggLife
Hi. Im using mysql and I have a 'date' column in my 'Logs' table that is a timestamp. I tried to write this code to display the month as a string of characters rather than two numbers. Like usual i cant get it to work. Any help would be greatly appreciated.

Code: Select all

<?php   
    session_start();
    include('connectDB.php');
    //in the query below, 'date' is a mysql timestamp it goes 'yyy-mm-dd hh:mm:ss' 
    $query3 = 'SELECT MAX(date) FROM Logs WHERE id IN (SELECT id FROM Users WHERE id ='.$_SESSION['id'].')';
    $result = mysql_query($query3);
    $row = mysql_fetch_array($result);
    
        $when = explode(' ',$row);
    
    //this is where things go wrong. It does not give me what i want?? I threw in the echo just to see whats going on
            echo $when[0];
            echo $when[1];
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////   //////////////
    
    $date = $when[0];
    
    $date2 = explode('-',$date);
    
    $year = $date[0];
    $month = $date[1];
    $day = $date[2];
        
    $months[0] = 'January';
    $months[1] ='Febuary';
    $months[2] = 'March';
    $months[3] = 'April';
    $months[4] = 'May';
    $months[5] = 'June';
    $months[6] = 'July';
    $months[7] ='August';
    $months[8] ='September';
    $months[9] = 'October';
    $months[10] ='November';
    $months[11] ='December';
    
    //supposed to display the name of the month
    echo $months[$month]; 
?>

Re: algorithm to display month from timestamp doesnt work???

Posted: Thu Nov 13, 2008 2:45 am
by Mark Baker
Make sure you're getting the correct value in $when first

Code: Select all

<?php   
    session_start();
    include('connectDB.php');
    //in the query below, 'date' is a mysql timestamp it goes 'yyy-mm-dd hh:mm:ss' 
    $query3 = 'SELECT MAX(date) as maxDate FROM Logs WHERE id IN (SELECT id FROM Users WHERE id ='.$_SESSION['id'].')';
    $result = mysql_query($query3);
    $row = mysql_fetch_array($result);
        $when = $row['maxDate'];
?>

Re: algorithm to display month from timestamp doesnt work???

Posted: Thu Nov 13, 2008 3:09 am
by ThuggLife
Ya, I think the problem has something to do with using the explode() function on the timestamp field. I know the $when array isnt holding what it is supposed to. Can someone please confirm that the variables being returned from the mysql_fetch_array() function are strings.

Re: algorithm to display month from timestamp doesnt work???

Posted: Thu Nov 13, 2008 3:12 am
by VladSun
ThuggLife wrote:Ya, I think the problem has something to do with using the explode() function on the timestamp field. I know the $when array isnt holding what it is supposed to. Can someone please confirm that the variables being returned from the mysql_fetch_array() function are strings.
No, the mysql_fetch_array() return value is array type. That's what Mark Baker meant with his post.

Re: algorithm to display month from timestamp doesnt work???

Posted: Thu Nov 13, 2008 3:13 am
by VladSun
And instead of pasring the timestamp with PHP code, do it in the SQL query - http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html

Re: algorithm to display month from timestamp doesnt work???

Posted: Fri Nov 14, 2008 2:42 am
by ThuggLife
Ya, I know that the mysql_fetch_array() returns an array. I was asking if the values stored in the array would be strings.

Re: algorithm to display month from timestamp doesnt work???

Posted: Fri Nov 14, 2008 2:55 am
by Mark Baker
ThuggLife wrote:Ya, I know that the mysql_fetch_array() returns an array. I was asking if the values stored in the array would be strings.
That values stored in that array will be the appropriate datatype based on the database column datatypes: Varchars will return strings, numbers will return float or integer as appropriate, dates will return integer, etc