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 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problems with my date format

    Hi,

    Today is 11th December 2012

    I have my date stored in my MySql dbase as a datetime
    in this format: 2012-12-11 00:00:00

    I want to output it as m/d/y format.

    So I would like: 12/11/12


    I have tried this:

    PHP Code:
    $N_pub_date     "{$row['pub_date']}";
    $N_pub_date date("m/d/y"$N_pub_date); 

    echo 
    $N_pub_date 
    But I am getting this: 12/31/69

    Anyone know how I correct this problem ?

    Thanks


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    The problem is, the date function expects a timestamp as the second parameter and not a mysql date.

    Try this:

    PHP Code:
    $N_pub_date     strtotime("{$row['pub_date']}"); 
    $N_pub_date date("m/d/y"$N_pub_date);  

    echo 
    $N_pub_date 

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,854
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I have my date stored in my MySql dbase as a datetime
    in this format: 2012-12-11 00:00:00

    I want to output it as m/d/y format.

    So I would like: 12/11/12
    It'd better to use the function date_format() in mysql itself.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi,

    Thanks for replies.

    I tried:

    PHP Code:
    $N_pub_date     strtotime("{$row['pub_date']}"); 
    $N_pub_date     date("m/d/y"$N_pub_date); 
    But I still get: 12/31/69

    I looked at the MYSQL link and think I need this:

    PHP Code:
    SELECT DATE_FORMAT('2009-10-04 22:23:00''%m/%d/%Y'); 
    But how do write that into my php code ?

    Thanks
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I would suspect you have a cached page there. If pub_date is 2009-10-04 22:23:00, that is valid.
    The DATE_FORMAT is used in SQL, not in PHP. Simply modify the query and add the DATE_FORMAT(pub_date, '%m/%d/%Y') AS formattedDate, then pull the formattedDate from the resultset.

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    So my query is :
    PHP Code:
    $sql "SELECT * FROM pages WHERE page_id = $Db_page_id AND client_no = '$this_client' "
    So if I want to include

    DATE_FORMAT(pub_date, '%m/%d/%Y') AS formattedDate

    then I'll have to list all the records I guess ??
    ( there are about 18 of them )

    It seems to me it would be nicer to just reformat the result for that record.

    Something like :

    PHP Code:
    $N_pub_date     strtotime("{$row['pub_date']}"); 
    $N_pub_date     date("m/d/y"$N_pub_date); 
    Can it not be done with php ??

    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This is fine:
    PHP Code:
    $N_pub_date     strtotime("{$row['pub_date']}"); 
    $N_pub_date     date("m/d/y"$N_pub_date); 
    So long as $row['pub_date'] represents a valid datetime string in SQL and strtotime is available (which it has been since like, 4.2 or something like that). So if I do this:
    PHP Code:
    $row = array('pub_date' => '2009-10-04 22:23:00');
    $N_pub_date     strtotime("{$row['pub_date']}"); 
    $N_pub_date     date("m/d/y"$N_pub_date);
    printf('Date: %s'$N_pub_date); 
    I get this:
    Code:
    Date: 10/04/09
    As for SQL, although I'd suggest querying for each property separately, you can mix and match wildcards with explicits. SELECT *, DATE_FORMAT(pub_date, '%m/%d/%Y') AS formattedDate FROM. . . is fine.

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks

    I have it working now
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

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