Formatting date returned from MySQL query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    Formatting date returned from MySQL query

    Hello,

    Using PHP 4, MySQL 4, I am getting a date field

    $dbh = executeSQL($que ry);
    while ($row = mysql_fetch_arr ay($dbh)) {
    $offer_id = $row['offer_id'];
    $dep_date = $row['depart_day'];
    // print out date
    print $dep_date;

    THe problem is the date prints out in this form "2006-02-05 00:00:00"
    and I want it to print out like this "02/22/2006".

    How can I do this? I would prefer to use a PHP function as opposed to
    applying some MySQL date function prior to extracting the result.

    Thanks, - Dave

  • Michael Austin

    #2
    Re: Formatting date returned from MySQL query

    laredotornado@z ipmail.com wrote:
    [color=blue]
    > Hello,
    >
    > Using PHP 4, MySQL 4, I am getting a date field
    >
    > $dbh = executeSQL($que ry);
    > while ($row = mysql_fetch_arr ay($dbh)) {
    > $offer_id = $row['offer_id'];
    > $dep_date = $row['depart_day'];
    > // print out date
    > print $dep_date;
    >
    > THe problem is the date prints out in this form "2006-02-05 00:00:00"
    > and I want it to print out like this "02/22/2006".
    >
    > How can I do this? I would prefer to use a PHP function as opposed to
    > applying some MySQL date function prior to extracting the result.
    >
    > Thanks, - Dave
    >[/color]


    I am just curious as to why newbies (and some experienced folks as well) ask
    questions here that can readily be found 1) using google and 2) using the
    documentation that has been developed for PHP and MySQL and not have to wait an
    hour or more before someone replies...

    Dave the answers to ALL of your "date" questions can be found at:
    http://us2.php.net/date and most of your questions in general about PHP is at
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    --
    Michael Austin.
    DBA Consultant
    Donations welcomed. Http://www.firstdbasource.com/donations.html
    :)

    Comment

    • cvanschalkwijk@gmail.com

      #3
      Re: Formatting date returned from MySQL query

      Dave,

      $timestamp = strtotime($row['depart_day']);
      $dep_date = date("m/d/Y",$timestam p);

      You want to convert the mysql datetime to a timestamp with strtotime
      http://us2.php.net/manual/en/function.strtotime.php and then pass it
      into date() with the format you want.

      - Clay

      Comment

      Working...