SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)

    PHP Function of the Day (2011-9-27): date_parse

    One of the more popular functions in PHP is strtotime. Occassionally I see it paired with date to find out a month or day. This is inefficient compared to using today's function - date_parse

    Date parse returns an array about the date, or false if the date is invalid. As of PHP 5.4 you'll be able to use array offsets on the return to grab the month, day or year immediately

    Code php:
    <?php
    // Example of offset access available as of PHP 5.4
    $day = date_parse('2011-03-04')['day'];

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    date_parse() only parses the supplied date string into its component parts, it is not intended to do the same thing as date('...', strtotime('...')).

    P.S. Your example won't work, 'Today' contains no day part.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,033
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Fixed. That's odd, the instructions imply it takes the same arguments as strtotime.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    It does take the same input as strtotime, your mistake was in expecting the result to have all of the resultant array items populated according to the date calculated from the input string.

    It doesn't help that the only example in the manual provides all of the date/time parts in the input.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Member
    Join Date
    Feb 2010
    Location
    Arkansas, USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    date_parse() is not very useful to me because I'm always needing the timestamp. I might use it more often if part of the returned array was the timestamp. But I guess that's what strtotime is for.


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
  •