Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts

    Modifying Date output

    This should be an easy one for the experts...

    I want to modify a date i am echoing from my sql database

    This is what is stored in my sql database
    2008-10-13 13:32:40

    I would like to echo it like this
    2008-10-13

    I've never done it so any help is appreciated. Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You can specify this in your query. Check out MySQL's DATE_FORMAT function

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,024
    Thanks
    2
    Thanked 314 Times in 306 Posts
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    so does this mean now i have to make a separate query just to concat the date?

    why can't i do something like this... (this doesn't work though)

    date("F/j/Y",$row['job_create_date']) \\job_create_date = 2008/11/02 4:30:20;
    Last edited by macleodjb; 12-13-2008 at 06:16 PM.

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    No, you shouldn't need a separate query to pull a formatted date.

    Quote Originally Posted by macleodjb View Post
    date("F/j/Y",$row['job_create_date']) \\job_create_date = 2008/11/02 4:30:20;
    That doesn't work because the date() function expects a timestamp as the second argument.

    If you want to go that route, you would first need to convert your $row['job_create_date'] using strtotime(). However, it would be more efficient to let MySQL pull the formatted date, rather that pulling and unformatted date and formatting it in php.

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    317
    Thanks
    24
    Thanked 3 Times in 3 Posts
    how do you add that to my original query? I'm not sure how to do it.

    PHP Code:
    SELECT FROM `job_detailWHERE (`job_cat2` = '$id' && `job_ended` != '1')

  • #7
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    If you just need the date portion without changing it's format:
    Code:
    SELECT *, DATE(`job_create_date`) AS just_date FROM `job_detail` WHERE (`job_cat2` = '$id' && `job_ended` != '1');
    Your date will be accessible via $row['just_date'] (based on your previous example)

    If you'd like to change the format of the date (to say Nov 2, 2008):
    Code:
    SELECT *, DATE_FORMAT(`job_create_date`, '%b %e, %Y') AS formatted_date FROM `job_detail` WHERE (`job_cat2` = '$id' && `job_ended` != '1');
    Your date will be accessible via $row['formatted_date'] (based on your previous example)

  • Users who have thanked PappaJohn for this post:

    macleodjb (12-14-2008)


  •  

    Posting Permissions

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