Convert time() output to certain formate.

Discussion in 'PHP' started by MacPC, May 20, 2007.

  1. #1
    Hello,

    I am new to this PHP but I am learning.

    I want to be able to store the time visitor come to my site to MySQL.

    When I use the time(), it returns a string of numbers of date and time. What I want to do is to seperate the time and the date portion of the string into two parts, and I want to reformat the time into something like 13:25, (24-hour format is fine.) so that I can store in two columns time: 13:25 and date 5/20/2007. How do I do that? Are there and predefined functions in php can do that?

    Thanks in advance for answering such a dumb question.

    MacPC.
     
    MacPC, May 20, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Have a look at date().
    
    $timestamp = time();
    
    date('H:i', $timestamp); // 13:25
    date('m/d/Y', $timestamp); // 5/20/2007
    
    PHP:
     
    nico_swd, May 20, 2007 IP
  3. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #3
    as far i know date returns the current time if no timesstaamp is used.
    so MacPC,you can simply use it:

    $time=date('H:i');
    $date=date('m/d/y');

    later you just echo $time and $date where you need them
     
    coderbari, May 21, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    You are right, but I did it this way to show him an example on how to use date() with timestamps in general, because he wants to use timestamps from a database, and not the current one.
     
    nico_swd, May 21, 2007 IP
  5. MacPC

    MacPC Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks everyone. It helps. :)

    MacPC
     
    MacPC, May 21, 2007 IP
  6. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #6
    well he wants to store the time and date in the database
    where did he tell he want to use the use the timestamp from database?
     
    coderbari, May 21, 2007 IP