#1
  1. DEVILS IN RED
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Kerala, India
    Posts
    200
    Rep Power
    10

    PHP strtotime(), parsing order of date formats


    I check for this on google, could not find a good guide in details about strtotime datestrings. The manual siad it uses the same Unix datetime formats but I could not find what i was looking for in docs for Unix as well.

    My question is,
    What is the order in which strtotime determines which format a given datetime string is in?

    Let me elaborate.
    Suppose my code says
    Code:
    strtotime('12/1/2012');
    I assume strotime() will first identify the date format used by comparing it with valid dateformats. What I want to know is the actual order of comparison. For eg. if the above example will be parsed as December 1st or January 12th.

    Thanks in advance.
    Ahmed Shefeer
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    so during your research, it never occured to you that it might be a good idea to look into the manual?

    Originally Posted by the manual
    Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
    The various other formats can be found behind a link on the same page.

    Either way: Why on earth would you wanna rely on a default behaviour you couldn't even find out with a Google search? Isn't that a pretty clear warning sign that you should use an explicit format instead?

    DateTime::createFromFormat()

    Comments on this post

    • Shefeer agrees : Was in a hurry looking through the manual. Did not notice it
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. DEVILS IN RED
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    Kerala, India
    Posts
    200
    Rep Power
    10
    Is there a way to do the same thing in javascript? I mean something similar to DateTime::createFromFormat() in php?

    I am not sure this should be asked in the javascript forums instead, but this question is so closely related to the above question.
    Ahmed Shefeer
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by Shefeer
    Is there a way to do the same thing in javascript?
    No. You'd have to parse the custom date string yourself with a regex and then pass the year, month etc. to the Date constructor.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo