SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with date_format function in mysql

    I have table with set type as datetime when I insert record in this table, I can use now() to set current date and time of this field.

    For example of data keep is this field is.
    2001-08-10 23:55:29

    When I want to format this field for easy to read I can type query in phpmyadmin like this

    select date_format(now,'%e %b %Y') from mytable
    result is : 10 Aug 2001

    select date_format(now,'%H:%i) from mytable
    result is : 23:55

    and if i want to select date and time I can use
    select date_format(now,'%e %b %Y %H:%i') from mytable
    result is : 10 Aug 2001 23:55

    No problem with these select but when I want to apply to my website and write php script, it has problem.
    Please look my source code

    PHP Code:
            $query "select ID,Lastpost from $questable";
            
    $result mysql_db_query("$dbname",$query);        
            if (
    $result
            {
                while (
    $row=mysql_fetch_array($result))
                {
                    
    $ID=$row['ID'];
                    
    $Lastpost=$row['Lastpost'];                        
                }
            } 
    This code Lastpost column set type as datetime which keep current date and time.

    I want to use date_format to query only date from Lastpost and keep in $nowdate and query only time from Lastpost keep in $nowtime. I try many time but not got errer message how can I do.
    I live in Thailand. My English grammar not well.

  2. #2
    SitePoint Zealot alexk's Avatar
    Join Date
    Nov 2000
    Location
    Sydney, Australia
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will this code work?...
    PHP Code:
    $query "SELECT ID, date_format(Lastpost,'%e %b %Y') AS nowdate, date_format(Lastpost,'%H:%i) AS nowtime  FROM $questable";
            
    $result mysql_db_query("$dbname",$query);        
            if (
    $result
            {
                while (
    $row=mysql_fetch_array($result))
                {
                    
    $ID=$row['ID'];
                    
    $nowdate $row['nowdate'];
                    
    $nowtime $row['nowtime']; 
                }
            } 
    Muspy - follow your favourite artists!

  3. #3
    SitePoint Addict itsource's Avatar
    Join Date
    Jun 2001
    Location
    Thailand
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,alexk this code work.
    I live in Thailand. My English grammar not well.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •