Php 구체화하십시오 형식으로 할 경우 'mm dd yy' (예를 들어, 10 16 2003년) 에 있는 '어떻게' 그리고 'datetime' 제대로 변환하시겠습니까 날짜 형식으로 년-월-일 '?' 유일한 이유는 모두 요구할 수 있기 때문이다 '' 한 '와' DateTime 데이트였어 데릭쉐퍼드와 별색, 다른 한 다른 별색.
'다음' () '를 사용하여 스트라토타임 날짜 (& # 39, Y-m-d& # 39;)' 첫 데이트였어 변환할지 다시:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
참고 할 수 있다는 것을 사용하는 것의 차이를 슬래시 "/ '및 하이픈입니다' - '의 ' 스트라토타임 () ' 함수. Php.net 에서 쿼트에.
>. 날짜는 m / d / y 또는 d m y 형식식 >. 해석되는 살펴보고 있다. >. 다양한 사이에 분리자의 >. 구성 요소: 만약 seperator 는 >. 슬래시 (/), 그리고 미국 m / d / y 는 >. 사용됩니다. 반면 seperator 는 경우 >. 대시 (-) 또는, 그 점 (.) >. 유럽 d m y 형식은 가정합니다.
날짜, 구문 분석을 위해 콩지름에: Datetime 크리스 프롬포르마 ();
예:
$dateDE = "16/10/2013";
$dateUS = \DateTime::createFromFormat("d.m.Y", $dateDE)->format("m/d/Y");
그러나 신중한 있으므로 이는 충돌 호스트당:
PHP Fatal error: Call to a member function format() on a non-object
사실 당신은 서식을 확인할 필요가 있는 미세한 먼저 갔다.
$dateDE = "16/10/2013";
$dateObj = \DateTime::createFromFormat("d.m.Y", $dateDE);
if (!$dateObj)
{
throw new \UnexpectedValueException("Could not parse the date: $date");
}
$dateUS = $dateObj->format("m/d/Y");
이제 추락한 예외를 잡을 수 있는 대신, 전파 등을 얻을 수 있습니다.
잘못된 형식, 16.10.2013 는 $ 다테데 " " 한다;;;
이 경우 미리, t 어졌다면 dd yy mm 포맷하십시오 won& # 39 로 일하는 것으로 나타났다. 아래 문서를 그들은 미리 가이드라인을 제시했다.
>. 날짜는 m / d / y 또는 d m y 형식은 해석되는 보면서 >. 다양한 구성 요소 사이의 분리자를. 만약 seperator 는 >. 슬래시 (/), 그리고 미국 m / d / y 가 사용됩니다. 반면 경우 >. seperator 는 대시 (-) 또는 유럽 d m y 관심용 포맷하십시오 점 (.) >. 간주됩니다.
그러니까 그냥 can& # 39, t use as 프로파일링하려는. Yyyy / mm / dd 포맷으로 사용하려고 할 경우 거짓값 제거하시겠습니까 그런 것입니다. 면변형 다음 수 있습니다.
$date = "23/02/2013";
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
$timestamp = strtotime(str_replace('/', '-', $date));
}
echo $timestamp; // prints 1361577600
우리는 같은 날짜 " 07/May/2018"; 날짜 및 ". 07-05-2018 " 필요하다. mysql 호환적 있다
if (!empty($date)) {
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
$timestamp = strtotime(str_replace('/', '-', $date));
}
$date = date('Y-m-d', $timestamp);
}
작동하잖아 for me. 경험하십시오:)
(월, 일, 년) 을 사용하여 원하는 경우 미국 유럽연합 (eu) 의 스타일을 오더할 동의하려면 날짜 형식 (대시나 시기에 사용하여 날, 달, 년) , 그대로 유지하면서 다른 형식으로 DateTime 클래스용 확장할 수 있습니다.
/**
* 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'
또는 y 메가 d 동의하려면 메가 d y 및 출력 기능을 할 수 있습니다.
/**
* 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:
<?= asYmd('7-24-2019') ?>
// or
<?php echo asYmd('7-24-2019'); ?>