Hướng dẫn get date from string php - lấy ngày từ chuỗi php

Nếu bạn muốn chấp nhận ngày sử dụng đặt hàng của Mỹ (tháng, ngày, năm) cho các định dạng kiểu châu Âu (sử dụng dấu gạch ngang hoặc thời gian, tháng, tháng, năm) trong khi vẫn chấp nhận các định dạng khác, bạn có thể mở rộng lớp DateTime:while still accepting other formats, you can extend the DateTime class:

/**
 * Quietly convert European format to American format
 *
 * Accepts m-d-Y, m-d-y, m.d.Y, m.d.y, Y-m-d, Y.m.d
 * as well as all other built-in formats
 * 
 */
class CustomDateTime extends DateTime 
{
  public function __construct(string $time="now", DateTimeZone $timezone = null) 
  {
    // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y (substr avoids microtime error)
    $time = str_replace(['-','.'], '/', substr($time, 0, 10)) . substr($time, 10 );

    parent::__construct($time, $timezone);
  }
}

// usage:
$date = new CustomDateTime('7-24-2019');
print $date->format('Y-m-d');

// => '2019-07-24'

Hoặc, bạn có thể tạo một chức năng để chấp nhận M-D-Y và đầu ra Y-M-D:

/**
 * Accept dates in various m, d, y formats and return as Y-m-d
 * 
 * Changes PHP's default behaviour for dates with dashes or dots.
 * Accepts:
 *   m-d-y, m-d-Y, Y-m-d,
 *   m.d.y, m.d.Y, Y.m.d,
 *   m/d/y, m/d/Y, Y/m/d,
 *   ... and all other formats natively supported 
 * 
 * Unsupported formats or invalid dates will generate an Exception
 * 
 * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
 * @param  string $d various representations of date
 * @return string    Y-m-d or '----' for null or blank
 */
function asYmd($d) {
  if(is_null($d) || $d=='') { return '----'; }

  // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
  $d = str_replace(['-','.'], '/', $d);

  return (new DateTime($d))->format('Y-m-d');
}

// usage:



// or


Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    strtotime() – This is basically a function which returns the number of seconds passed since Jan 1, 1970, just like a linux machine timestamp. It returns the number of seconds passed according to the parameter passed to the function.
    Syntax

     strtotime(parameter);

    Bàn luận

    • Time/Date
    • now(optional)

    Chuyển đổi chuỗi cho đến ngày và DateTime sử dụng một số hàm/phương thức như strtotime (), getDate (). Chúng ta sẽ thấy những gì các chức năng này làm.strtotime () - về cơ bản đây là một hàm trả về số giây được truyền kể từ ngày 1 tháng 1 năm 1970, giống như dấu thời gian của máy Linux. Nó trả về số giây được truyền theo tham số được truyền cho hàm.syntax Returns the number of seconds passed since Jan 1, 1970.

    Tham số This function return the date/time information of the passed parameter(date/time);
    Syntax

    getDate(parameter);

    Loại trả lại trả về số giây trôi qua kể từ ngày 1 tháng 1 năm 1970. The parameter is optional as it takes the current local time as default parameter.
    Return Type It returns the information of the date, day, year, month etc in an array.

    getDate () Hàm này trả về thông tin ngày/giờ của tham số được truyền (ngày/giờ); cú pháp

    Tham số Tham số là tùy chọn vì nó mất thời gian địa phương hiện tại làm tham số mặc định. Loại return, nó trả về thông tin của ngày, ngày, năm, tháng, v.v. trong một mảng.

    Mã để chuyển đổi một chuỗi cho đến nay

     strtotime(parameter);
    1
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    5
     strtotime(parameter);
    3

     strtotime(parameter);
    4

    Output:

    Array
    (
        [seconds] => 0
        [minutes] => 0
        [hours] => 0
        [mday] => 21
        [wday] => 6
        [mon] => 5
        [year] => 2011
        [yday] => 140
        [weekday] => Saturday
        [month] => May
        [0] => 1305936000
    )
    

    $time_input

    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    1
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    2
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    3
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    4

    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    5
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    7
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    2$time_input
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    4

    Mã để chuyển đổi chuỗi thành DateTime

     strtotime(parameter);
    6
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
     strtotime(parameter);
    8
     strtotime(parameter);
    9

     strtotime(parameter);
    4

    Output:

    10/Jun/2011 07:00:02
    

    getDate(parameter);
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    1
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    2
     strtotime(parameter);
    6
    getDate(parameter);
    5
    We can use “D” in the place of “d” for getting the day in the output

    getDate(parameter);
    6
    getDate(parameter);
    7__12

    Mã để chuyển đổi chuỗi thành DateTime

     strtotime(parameter);
    6
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
     strtotime(parameter);
    8
     strtotime(parameter);
    9

     strtotime(parameter);
    4

    Output:

    Tue/May/2011 03:00:02
    

    getDate(parameter);
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    1
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    2
     strtotime(parameter);
    6
    getDate(parameter);
    5
    We can use “H” in the place of “h” for getting the time in 24 Hour format in the output

    getDate(parameter);
    6
    getDate(parameter);
    7__12

    Mã để chuyển đổi chuỗi thành DateTime

     strtotime(parameter);
    6
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
     strtotime(parameter);
    8
     strtotime(parameter);
    9

     strtotime(parameter);
    4

    Output:

    Tue/May/2011 15:00:02
    

    getDate(parameter);
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    0
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    1
    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    
    
    // or
    
    
    
    2
     strtotime(parameter);
    6
    getDate(parameter);
    5

    getDate(parameter);
    6
    getDate(parameter);
    7__12


    Làm thế nào tôi có thể nhận được ngày hiện tại trong chuỗi PHP?

    Làm điều này: $ date = ngày ("D M D, Y G: I");Bạn sẽ có ngày hiện tại trong biến ngày $, dưới dạng chuỗi - không cần bất kỳ hoạt động bổ sung nào.$date = date("D M d, Y G:i"); You'll have the current date in the $date variable, as a string -- no need for any additional operation.

    Strtotime có nghĩa là gì trong PHP?

    Hàm strtotime () phân tích dữ liệu văn bản tiếng Anh vào dấu thời gian unix (số giây kể từ ngày 1 tháng 1 năm 1970 00:00:00 gmt).Lưu ý: Nếu năm được chỉ định ở định dạng hai chữ số, các giá trị trong khoảng 0-69 được ánh xạ tới 2000-2069 và các giá trị trong khoảng 70-100 được ánh xạ tới 1970-2000.parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

    Hàm ngày php là gì?

    Hàm ngày/giờ PHP.

    Làm cách nào để định dạng một ngày thành một chuỗi?

    Hãy xem mã đơn giản để chuyển đổi ngày thành chuỗi trong java ...
    Ngày ngày = lịch.getInstance (). GetTime () ;.
    Dateformat dateformat = new SimpleDateFormat ("yyyy-mm-dd hh: mm: ss") ;.
    Chuỗi strdate = dateformat.format (ngày) ;.