Results 1 to 13 of 13

Thread: Format PHP Date

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Format PHP Date

    I save dates to the database in the following format - "1979-12-28"

    When displaying them on the website I want to display it as "28 Dec 1979" so I tried this:

    echo "Born:" . date("d M Y",$myrow["DOB"]) . "<br>";

    but that converts "1979-12-28" to "Born:31 Dec 1969"

    What am I doing wrong?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Format PHP Date

    Are you using MySql? Databases often store dates in a different format. In MySql can convert the date in your database to a UNIX timestamp inside your query:
    Code:
     SELECT FROM table UNIX_TIMESTAMP(date) date;
    Once this is done you can use it in the date function.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: Format PHP Date

    Thanks for your response. I'd rather keep it stored in my MYSQL database in date format for ease of use.

    Isn't there anyway to convert "1979-12-28" to "28 Dec 1979"?

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Format PHP Date

    Its still going to be stored in date format. You will be converting it to the format PHP understands inside the SELECT query, it won't change the format in which the data is stored in your database.

    Post your query and I'll show you what I mean if you still don't follow.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Re: Format PHP Date

    Yes, the timestamp argument "$myrow["DOB"]" is in the wrong format.
    string date ( string format [, int timestamp] )

    Try:
    echo "Born:" . date("d M Y",mktime(0, 0, 0, substr($myrow["DOB"],5,2), substr($myrow["DOB"],8,2), substr($myrow["DOB"],0,4))) . "<br>";

    Edit: Actually, changing the query may be cleaner, but you'll need say an alias like UNIX_TIMESTAMP(Your_DOB_column) AS DOB_AsTimeStamp and use $myrow["DOB_AsTimeStamp"]
    echo "Born:" . date("d M Y",$myrow["DOB_AsTimeStamp"]) . "<br>";
    Last edited by Phenix; Jan 21st, 2005 at 05:08 PM. Reason: Clarify PHP timestamp argument; Added "Actually" comment
    Circa 1995
    Engineer - I think we should put our website address on our paper catalogs.
    Vice President - Don't get too excited about this internet thing.


    I am sorry, but the Oracle was mistaken. You cannot help us.
    -Matrix video game


    I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
    -Linus


    Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.

    That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".


    Are you threatening me, Master Jedi?
    -Chancellor Palpatine

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: Format PHP Date

    Thanks Phenix.

    Used:

    <b>Born: </b> <?php echo date("d M Y",mktime(0, 0, 0, substr($DOB,5,2), substr($DOB,8,2), substr($DOB,0,4))); ?> <br>

    and it worked a treat.

    Thanks for your help also visualAd

  7. #7
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: Format PHP Date

    You could try the strtotime() function.

    It would look something like this:
    PHP Code:
    echo 'Born: ' date('d M Y',strtotime($myrow['DOB'])) . '<br />'
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Format PHP Date

    Quote Originally Posted by JamesNZ
    Thanks Phenix.

    Used:

    <b>Born: </b> <?php echo date("d M Y",mktime(0, 0, 0, substr($DOB,5,2), substr($DOB,8,2), substr($DOB,0,4))); ?> <br>

    and it worked a treat.

    Thanks for your help also visualAd
    James,

    Most databases store dates in a different format, all these databases have the functions requied to convert form their native date types to unix timestamps.

    Its all very well using the solution posted by Penix, but it will be both slower and will break should MySql one day decide to change thier date format or you need to use another database, it will break. So what at first, looks good and easy to use, in the long run it will probably give you a headache.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Posts
    126

    Re: Format PHP Date

    Quote Originally Posted by visualAd
    James,

    Most databases store dates in a different format, all these databases have the functions requied to convert form their native date types to unix timestamps.

    Its all very well using the solution posted by Penix, but it will be both slower and will break should MySql one day decide to change thier date format or you need to use another database, it will break. So what at first, looks good and easy to use, in the long run it will probably give you a headache.
    What solution would you suggest visualAd?

  10. #10
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    Re: Format PHP Date

    I think this is what he is suggesting:

    Quote Originally Posted by visualAd
    Are you using MySql? Databases often store dates in a different format. In MySql can convert the date in your database to a UNIX timestamp inside your query:
    Code:
     SELECT FROM table UNIX_TIMESTAMP(date) date;
    Once this is done you can use it in the date function.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  11. #11
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Format PHP Date

    Quote Originally Posted by JamesNZ
    What solution would you suggest visualAd?
    The one I posted which involves doing the conversion inside the SELECT query using the UNIX_TIMESTAMP() function. This will be quicker and more efficient than using PHP's string manipulation functions and make your code mroe portable.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  12. #12
    Addicted Member
    Join Date
    Feb 2006
    Posts
    185

    Re: Format PHP Date

    I need It, But from 2005 This problem not solved !!!!!!!!!!
    How can I show a date in the format of 11/12/2007 from database?

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Format PHP Date

    Start your own thread for your own questions please.

Posting Permissions

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



Click Here to Expand Forum to Full Width