SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: date formatting

  1. #1
    SitePoint Zealot cherie's Avatar
    Join Date
    Nov 2004
    Location
    Roxbury, NY
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question date formatting

    I have read dozens of posts, manuals, etc on date formatting. I know exactly how I want my date to be formatted and all of the various options I have for formatting the date.

    But where do I put the information?

    My query is
    Code PHP:
    //used for default display of tours by date
    mysql_select_db($database_atours, $atours);
    $query_tourDate = "SELECT * FROM tours, artists WHERE tours.artist_id=artists.artist_id AND date > NOW() ORDER BY date ASC";
    $tourDate = mysql_query($query_tourDate, $atours);
    $row_tourDate = mysql_fetch_assoc($tourDate);
    $totalRows_tourDate = mysql_num_rows($tourDate);

    The two dates I need to format are coded like this
    Code PHP:
    <?php 
    echo "<span class='tour-head'>"; 
    echo $row_tourDate['date']; 
    ?>
     
    <?php 
    if(trim($row_tourDate['dateend']) != '0000-00-00'){ 
    // print out everything 
    echo " through "; echo $row_tourDate['dateend']; 
    }else{ 
    // don't do anything 
    } 
    ?>

    I want to include date('M, d, Y').
    I have also tried this in my query DATE_FORMAT(date, '%d %m %Y')

    Just not sure where to put the info.

    Your help is appreciated.
    Cherie
    foamyindustries.com
    Movies We Watch - we watch it and tell you what we think
    chickenflicker.com - everyone needs a chicken a day

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2007
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    //used for default display of tours by date
    mysql_select_db($database_atours, $atours);
    $query_tourDate = "SELECT * FROM tours, artists WHERE tours.artist_id=artists.artist_id AND date > NOW() ORDER BY date ASC";
    $tourDate = mysql_query($query_tourDate, $atours);
    $totalRows_tourDate = mysql_num_rows($tourDate);
     
     
    while($row_tourDate = mysql_fetch_assoc($tourDate)) {
     
     
     
        echo "<span class='tour-head'>"; 
        echo $row_tourDate['date']; 
     
        if(trim($row_tourDate['dateend']) != '0000-00-00'){ 
            // print out everything 
            echo " through "; echo $row_tourDate['dateend']; 
        }else{ 
            // don't do anything 
        } 
    }
    ?>

    Post all you code cause I dont fully understand your problem

  3. #3
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is dateend a unix timestamp or a MySQL date?

    For unix timestamp:
    echo date('M, d, Y', $row['dateend'];

    For MySQL date field:
    echo date('M, d, Y', strtotime($row['dateend']));
    (I think)

    Edit:
    PHP Code:
        if(trim($row_tourDate['dateend']) != '0000-00-00'){
            
    // print out everything
            
    echo " through "; echo $row_tourDate['dateend'];
        }else{
            
    // don't do anything
        

    can be written as
    PHP Code:
        if(trim($row_tourDate['dateend']) != '0000-00-00'){
            
    // print out everything
            
    echo " through "; echo $row_tourDate['dateend'];
        } 
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  4. #4
    SitePoint Zealot cherie's Avatar
    Join Date
    Nov 2004
    Location
    Roxbury, NY
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You AlienDev. It nows works and I now understand how it works.

    I am using dates pulled from the MySQL table. So echo date('M, d, Y', strtotime($row['dateend'])); works great.
    Cherie
    foamyindustries.com
    Movies We Watch - we watch it and tell you what we think
    chickenflicker.com - everyone needs a chicken a day

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Do the formatting within the SQL query itself:
    http://dev.mysql.com/doc/refman/5.0/...on_date-format

    DATE_FORMAT(date, '&#37;d %m %Y') AS date
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,813
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Formatting your output in the SQL before passing it back to the PHP is more efficient and uses less code.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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