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 //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
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
// 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 //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 //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 //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 //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
// 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 //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 //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 //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
4Mã để 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 //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
0strtotime[parameter];8
strtotime[parameter];9
strtotime[parameter];4
Output:
10/Jun/2011 07:00:02
getDate[parameter];
0
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 //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 //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 //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
strtotime[parameter];
6getDate[parameter];
5
We can use “D” in the place of “d” for getting the day in the output
// 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 //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 //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
2strtotime[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
// 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 //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 //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
2strtotime[parameter];6
getDate[parameter];5
getDate[parameter];6
getDate[parameter];7__12