algorithm to display month from timestamp doesnt work???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThuggLife
Forum Newbie
Posts: 13
Joined: Wed Oct 08, 2008 9:10 pm

algorithm to display month from timestamp doesnt work???

Post 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]; 
?>
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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'];
?>
ThuggLife
Forum Newbie
Posts: 13
Joined: Wed Oct 08, 2008 9:10 pm

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

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
ThuggLife
Forum Newbie
Posts: 13
Joined: Wed Oct 08, 2008 9:10 pm

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

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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
Post Reply