[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Convert this string to timestamp PHP

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #10184
    user2876368
    Participant

    I have this string: “13/10 15:00” and I would like to convert it to timestamp but when I do this:

          $timestamp = strtotime("13/10 15:00");
    

    It returns an empty value.

    #10185
    lajos-veres
    Participant
      $timestamp = strtotime("13-10-2013 15:00");
    

    This can be important:

    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.

    To avoid potential ambiguity, it’s best to use ISO 8601 (YYYY-MM-DD)
    dates or DateTime::createFromFormat() when possible.

    https://www.php.net/strtotime

    #10186
    user1864610
    Participant

    In your code strtotime() is attempting to convert 13/10 as the tenth day of the 13th month, which returns an error.

    If you want to parse a date string with a custom format, it’s better to use DateTime::createFromFormat() instead:

    $dtime = DateTime::createFromFormat("d/m G:i", "13/10 15:00");
    $timestamp = $dtime->getTimestamp();
    
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.