날짜변환

2021. 12. 29. 15:47·Development/PHP

날짜를 변환하고 싶을 때 아래 처럼 해주면 된다.

기본

try {
	$strDate = new DateTime($strDate);
 } catch(Exception $e) {
	echo"<br>\n 날짜변환 오류";
}
$strDate = @date_format($strDate, 'Y-m-d');

 

@date_format 에서 원하는 양식으로 날짜를 변환할 수 있다. Y-m-d로 변환하면 2022-06-30이 나오게 된다.

ex. Y.m.d => 2022.06.30 / Ymd => 20220630 / ymd => 220630 ...

 

# 2021-12-22T11:00:44+00:00

# 2021-12-22T11:00:44+00:00

// 1
$strDate = explode('+', $strDate[1]);
$strDate = trim($strDate[0]);
$strDate = str_replace("T"," ",$strDate);

// 2
$strDate = "";
$strDate = explode('datetime="', $strBody);
$strDate = explode('"', $strDate[1]);
$strDate = trim($strDate[0]);
echo "\r\nstrDate: ".$strDate;
try {
	$strDate = new DateTime($strDate);
	$strDate = date_format($strDate, 'Y-m-d');
} catch(Exception $e) {
}
//$strDate = trim(strip_tags($strDate[0]));
$arrData["Date"] = $strDate;
# Date 형식 : 20211229
$strInit_sd_day = strtotime($strInit_sd_day_start);
$strInit_sd_day = date('Y/m/d',$strInit_sd_day);

#출력 : 2021/12/29

on Apr 14, 2022

$strDate ="";
				$strDate = explode('</a>            on', $strHtmlListBody);
				$strDate = explode(' , by', $strDate[1]);
				$strDate = trim($strDate[0]);
				$strDate = str_replace(",","",$strDate);
				try {
							$strDate = new DateTime($strDate);
						} catch(Exception $e) {
							echo"<br>\n [2015] date $strCommentDate 이상징후";
							reset($arrData);


							continue;
						}
						$strDate = @date_format($strDate, 'Y-m-d');
						echo "strDate: {$strDate}";
				$arrData["Date"] = $strDate;
				print_r($arrData);
				exit;
// Dec 9, 2019 at 12:14 PM

$strDate = "";
$strDate = explode('<span style="float:left;">', $strList);
$strDate = explode('</span>', $strDate[1]);
$strDate = trim($strDate[0]);

echo"<br>\n strDate $strDate ";	
$strDate = str_replace("at ","",$strDate);
$strDate = str_replace(" PM","",$strDate);
$strDate = str_replace(" AM","",$strDate);
$strDate = explode(" ",$strDate); // 0:일, 1:월, 2:년, 3:시간

if ( substr_count($strDate[0], 'Jan')) {
	$strDate[0] = trim(str_replace("Jan", "01", $strDate[0]));
}else if ( substr_count($strDate[0], 'Feb')) {
	$strDate[0] = trim(str_replace("Feb", "02", $strDate[0]));
}else if ( substr_count($strDate[0], 'Mar')) {
	$strDate[0] = trim(str_replace("Mar", "03", $strDate[0]));
}else if ( substr_count($strDate[0], 'Apr')) {
	$strDate[0] = trim(str_replace("Apr", "04", $strDate[0]));
}else if ( substr_count($strDate[0], 'May')) {
	$strDate[0] = trim(str_replace("May", "05", $strDate[0]));
}else if ( substr_count($strDate[0], 'Jun')) {
	$strDate[0] = trim(str_replace("Jun", "06", $strDate[0]));
}else if ( substr_count($strDate[0], 'Jul')) {
	$strDate[0] = trim(str_replace("Jul", "07", $strDate[0]));
}else if ( substr_count($strDate[0], 'Aug')) {
	$strDate[0] = trim(str_replace("Aug", "08", $strDate[0]));
}else if ( substr_count($strDate[0], 'Sep')) {
	$strDate[0] = trim(str_replace("Sep", "09", $strDate[0]));
}else if ( substr_count($strDate[0], 'Oct')) {
	$strDate[0] = trim(str_replace("Oct", "10", $strDate[0]));
}else if ( substr_count($strDate[0], 'Nov')) {
	$strDate[0] = trim(str_replace("Nov", "11", $strDate[0]));
}else if ( substr_count($strDate[0], 'Dec')) {
	$strDate[0] = trim(str_replace("Dec", "12", $strDate[0]));
}
$strDate = $strDate[2]."-".$strDate[0]."-".$strDate[1]." ".$strDate[3];
//$strDate = strtotime($strDate);
echo"<br>\n strDate $strDate ";	
$arrData["Date"] = $strDate;
// 07 March 2013 at 4:50pm
$strDate = "";
$strDate = explode('<span style="float:left;">', $strList);
$strDate = explode('</span>', $strDate[1]);
$strDate = trim($strDate[0]);

echo"<br>\n strDate $strDate ";	
$strDate = str_replace("at ","",$strDate);
$strDate = str_replace("pm","",$strDate);
$strDate = str_replace("am","",$strDate);
$strDate = explode(" ",$strDate); // 0:일, 1:월, 2:년, 3:시간

if ( substr_count($strDate[1], 'January')) {
	$strDate[1] = trim(str_replace("January", "01", $strDate[1]));
}else if ( substr_count($strDate[1], 'February')) {
	$strDate[1] = trim(str_replace("February", "02", $strDate[1]));
}else if ( substr_count($strDate[1], 'March')) {
	$strDate[1] = trim(str_replace("March", "03", $strDate[1]));
}else if ( substr_count($strDate[1], 'April')) {
	$strDate[1] = trim(str_replace("April", "04", $strDate[1]));
}else if ( substr_count($strDate[1], 'May')) {
	$strDate[1] = trim(str_replace("May", "05", $strDate[1]));
}else if ( substr_count($strDate[1], 'June')) {
	$strDate[1] = trim(str_replace("June", "06", $strDate[1]));
}else if ( substr_count($strDate[1], 'July')) {
	$strDate[1] = trim(str_replace("July", "07", $strDate[1]));
}else if ( substr_count($strDate[1], 'August')) {
	$strDate[1] = trim(str_replace("August", "08", $strDate[1]));
}else if ( substr_count($strDate[1], 'September')) {
	$strDate[1] = trim(str_replace("September", "09", $strDate[1]));
}else if ( substr_count($strDate[1], 'October')) {
	$strDate[1] = trim(str_replace("October", "10", $strDate[1]));
}else if ( substr_count($strDate[1], 'November')) {
	$strDate[1] = trim(str_replace("November", "11", $strDate[1]));
}else if ( substr_count($strDate[1], 'December')) {
	$strDate[1] = trim(str_replace("December", "12", $strDate[1]));
}
$strDate = $strDate[2]."-".$strDate[1]."-".$strDate[0]." ".$strDate[3];
//$strDate = strtotime($strDate);
echo"<br>\n strDate $strDate ";	
$arrData["Date"] = $strDate;

 

# January 5th, 2021
$strDate = "";
$strDate = explode('<span class="Fz(18px) Lh(28px)">',$strHtmlListBody);
$strDate = explode('</span>',$strDate[1]);
$strDate = trim($strDate[0]);
$strDate = str_replace("th,",$strDate);
$strDate = explode(" ",$strDate); // 0:일, 1:월, 2:년, 3:시간
if ( substr_count($strDate[0], 'January')) {
	$strDate[0] = trim(str_replace("January", "01", $strDate[0]));
}else if ( substr_count($strDate[0], 'February')) {
	$strDate[0] = trim(str_replace("February", "02", $strDate[0]));
}else if ( substr_count($strDate[0], 'March')) {
	$strDate[0] = trim(str_replace("March", "03", $strDate[0]));
}else if ( substr_count($strDate[0], 'April')) {
	$strDate[0] = trim(str_replace("April", "04", $strDate[0]));
}else if ( substr_count($strDate[0], 'May')) {
	$strDate[0] = trim(str_replace("May", "05", $strDate[0]));
}else if ( substr_count($strDate[0], 'June')) {
	$strDate[0] = trim(str_replace("June", "06", $strDate[0]));
}else if ( substr_count($strDate[0], 'July')) {
	$strDate[0] = trim(str_replace("July", "07", $strDate[0]));
}else if ( substr_count($strDate[0], 'August')) {
	$strDate[0] = trim(str_replace("August", "08", $strDate[0]));
}else if ( substr_count($strDate[0], 'September')) {
	$strDate[0] = trim(str_replace("September", "09", $strDate[0]));
}else if ( substr_count($strDate[0], 'October')) {
	$strDate[0] = trim(str_replace("October", "10", $strDate[0]));
}else if ( substr_count($strDate[0], 'November')) {
	$strDate[0] = trim(str_replace("November", "11", $strDate[0]));
}else if ( substr_count($strDate[0], 'December')) {
	$strDate[0] = trim(str_replace("December", "12", $strDate[0]));
}
$strDate = $strDate[2]."-".$strDate[0]."-".$strDate[1];
//$strDate = strtotime($strDate);
echo"<br>\n strDate $strDate ";	
$arrData["Date"] = $strDate;

날짜 형식이 2021-05-1 등으로, 형식이 맞지 않을경우 (기본: 2021-05-01, 출력:2021-05-1)

date함수를 사용해서 Ymd형식으로 변환

$arrData["Date"] = date("Ymd",strtotime($strDate));

'Development > PHP' 카테고리의 다른 글

Passed variable is not an array or object  (0) 2021.12.31
MySQL 연결 및 한글 깨질 경우  (0) 2021.12.30
[정규표현식] 정규식 문자열 제거(영문/숫자)  (0) 2021.12.28
정규식 html 태그제거  (0) 2021.12.28
정규식 공백제거(str_replace, preg_replace)  (0) 2021.12.28
'Development/PHP' 카테고리의 다른 글
  • Passed variable is not an array or object
  • MySQL 연결 및 한글 깨질 경우
  • [정규표현식] 정규식 문자열 제거(영문/숫자)
  • 정규식 html 태그제거
곽진돔
곽진돔
Developer
  • 곽진돔
    echo "곽박한 세상";
    곽진돔
  • 전체
    오늘
    어제
    • 분류 전체보기 (200)
      • Development (67)
        • Linux (13)
        • k8s (3)
        • Docker (5)
        • AWS (1)
        • PHP (35)
        • Python (21)
        • Java (1)
        • SpringBoot (4)
        • JavaScript (1)
        • React (10)
        • MySql (19)
        • MongoDB (1)
      • Daily (5)
      • Study (7)
        • TIL (2)
        • license (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 글쓰기
    • 설정
  • 링크

    • github
  • 공지사항

  • 인기 글

  • 태그

    JavaScript
    크롤링
    Java
    인코딩
    chromedriver
    db
    php
    nodejs
    Linux
    SQL
    date
    윈도우
    리눅스
    error
    CentOS7
    정규표현식
    Selenium
    Shell
    스프링부트
    react
    IP
    springboot
    Python
    docker
    CentOS
    MySQL
    ssh
    HTML
    UTF8
    리액트
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
곽진돔
날짜변환
상단으로

티스토리툴바