SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    US
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert Timestamp to Date

    Hi... I have date stored in timestamp(14). I want to take the timestamp value and show it in a date format e.g. 27 Feb 2005 . How do I do that. Thanks for your help in advance.

  2. #2
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this (untested)...

    PHP Code:
    function mysql_to_date($timestamp)
    {
        
    $year substr($timestamp04);
        
    $month substr($timestamp51);
        
    $day substr($timestamp71);
        
    $hour substr($timestamp91);
        
    $minute substr($timestamp111);

        if(
    $month == '1')
        {
            
    $month "January";
        } elseif(
    $month == '2')
        {
            
    $month "February";
        } elseif(
    $month == '3')
        {
            
    $month "March";
        } elseif(
    $month == '4')
        {
            
    $month "April";
        } elseif(
    $month == '5')
        {
            
    $month "May";
        } elseif(
    $month == '6')
        {
            
    $month "June";
        } elseif(
    $month == '7')
        {
            
    $month "July";
        } elseif(
    $month == '8')
        {
            
    $month "August";
        } elseif(
    $month == '9')
        {
            
    $month "September";
        } elseif(
    $month == '10')
        {
            
    $month "October";
        } elseif(
    $month == '11')
        {
            
    $month "November";
        } elseif(
    $month == '12')
        {
            
    $month "December";
        }

                 return 
    $day." ".$month." ".$year;


  3. #3
    Internet Business Optimizer KCgame's Avatar
    Join Date
    Mar 2005
    Location
    Singapore
    Posts
    929
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to use date() to achieve your needs:
    PHP Code:
    $display=date('H\0\0\h\r\s  d M Y'$row['timestamp']);
    echo
    "$display";

    // will show something like 2300hrs 10 Jul 2005 etc. 
    Refer to the following for more details about the formatting:

    http://sg.php.net/manual/en/function.date.php

    Hope it helps.

  4. #4
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DA Master
    Try this (untested)...

    PHP Code:
    function mysql_to_date($timestamp)
    {
        
    $year substr($timestamp04);
        
    $month substr($timestamp51);
        
    $day substr($timestamp71);
        
    $hour substr($timestamp91);
        
    $minute substr($timestamp111);

        if(
    $month == '1')
        {
            
    $month "January";
        } elseif(
    $month == '2')
        {
            
    $month "February";
        } elseif(
    $month == '3')
        {
            
    $month "March";
        } elseif(
    $month == '4')
        {
            
    $month "April";
        } elseif(
    $month == '5')
        {
            
    $month "May";
        } elseif(
    $month == '6')
        {
            
    $month "June";
        } elseif(
    $month == '7')
        {
            
    $month "July";
        } elseif(
    $month == '8')
        {
            
    $month "August";
        } elseif(
    $month == '9')
        {
            
    $month "September";
        } elseif(
    $month == '10')
        {
            
    $month "October";
        } elseif(
    $month == '11')
        {
            
    $month "November";
        } elseif(
    $month == '12')
        {
            
    $month "December";
        }

                 return 
    $day." ".$month." ".$year;



    A switch would be a little cleaner, no?

    Use date here.

  5. #5
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it really matter about the switch? The if and switch statements both do the same thing. I did it quickly.

  6. #6
    SitePoint Addict DA Master's Avatar
    Join Date
    Apr 2004
    Location
    /etc/php.ini
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KCgame
    You might want to use date() to achieve your needs:
    PHP Code:
    $display=date('H\0\0\h\r\s  d M Y'$row['timestamp']);
    echo
    "$display";

    // will show something like 2300hrs 10 Jul 2005 etc. 
    Refer to the following for more details about the formatting:

    http://sg.php.net/manual/en/function.date.php

    Hope it helps.
    Sorry for bumping this. Thanks for the tip, your method is much easier, thank you.

  7. #7
    Internet Business Optimizer KCgame's Avatar
    Join Date
    Mar 2005
    Location
    Singapore
    Posts
    929
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem
    Well...i asked this question a couple of weeks ago


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
  •