SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict csj's Avatar
    Join Date
    Mar 2005
    Location
    South Africa
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    {Problem} How to display date?

    Im fairly new to PHP, but im pretty certain this is probably an easy problem. If it is then I apologise in Advance.

    I have a date saved in a database in the 2007-02-07 Format

    What I want to do is retrieve this from the database, but somehow break it up so that each part of the date becomes its own single variable.

    For example...

    $Year = "2007";
    $Month = "02";
    $Day = "07";

    I hope I have made this clear. I would really appreciate some help.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $date '2007-02-07';
    list(
    $year$month$day) = explode('-'$date); 
    That should do it.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Austin
    Posts
    401
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There are a few ways to do it.

    The easiest that I can see is to split it into an array and use it however you need. This method would work best as long as it is always in this format.

    PHP Code:
    $date date_from_mysql_query;

    $date split('-',$date);

    $year $date[0];
    $month $date[1];
    $day $date[2]; 
    You can also convert it to a date and display it using php. The strtotime() in conjunction with the date() function should be able to help you out put it this way.
    Merchant Equipment Store - Merchant Services, POS, Equipment, and supplies.
    Merchant Account Blog | Ecommerce Blog

  4. #4
    SitePoint Addict csj's Avatar
    Join Date
    Mar 2005
    Location
    South Africa
    Posts
    202
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a milllion!! I will try those out.

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    split() and explode() are the same thing, in case you wonder.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not really, in case you care

    PHP Code:
    print_r(split('.'"aa.bb.cc"));
    print_r(explode('.'"aa.bb.cc")); 

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Interesting!
    Ian Anderson
    www.siteguru.co.uk

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Fair enough. These are the same then:
    PHP Code:
    print_r(split('[.]'"aa.bb.cc"));
    print_r(explode('.'"aa.bb.cc")); 
    But explode is faster and it sounds cooler.

  9. #9
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Fair enough. These are the same then:
    PHP Code:
    print_r(split('[.]'"aa.bb.cc"));
    print_r(explode('.'"aa.bb.cc")); 
    But explode is faster and it sounds cooler.
    Save a character!
    PHP Code:
    print_r(split('\.'"aa.bb.cc"));
    print_r(explode('.'"aa.bb.cc")); 
    Not that you should use split for something so simple anyway.


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
  •