1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>날짜를 타임스탬프로 변환하고 싶을 때</title>
</head>
<body>
<div>
<?php
date_default_timezone_set('Asia/Seoul');
$year = 2015;
$month = 1;
$day = 1;
$hour = 10;
$minute = 30;
$second = 50;
echo "<p>mktime()함수:<br>";
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
echo "타임스탬프: " . $timestamp . "<br>";
echo "날짜로 변환 확인: " . date('Y/m/d H:i:s', $timestamp) . "<br><br>";
// 일시의 설정
$year = 2015;
$month = 12;
$day = 12;
$hour = 23;
$minute = 15;
$second = 30;
// mktime() 함수는 32비트 시스템에서 2038년 이후의 날짜를 처리할 수 없슴
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
echo "일시: {$year}년{$month}월{$day}일{$hour}시{$minute}분{$second}초<br>";
echo "타임스탬프: " . $timestamp . "<br>";
echo "날짜로 변환 확인: " . date('Y/m/d H:i:s', $timestamp) . "</p>";
// DateTime 클래스를 사용하면 32비트 시스템에서도 2038년 이후의 날짜를 처리할 수 있음
echo "<p>DateTime 클래스:<br>";
$date = new DateTime("$year/$month/$day $hour:$minute:$second");
// DateTime 클래스의 format() 메소드에 'U'를 지정하면 문자열에서 타임스탬프를
// 취득할 수 있음
$timestamp = $date->format('U');
echo "일시: {$year}년{$month}월{$day}일{$hour}시{$minute}분{$second}초<br>";
echo "타임스탬프: " . $timestamp . "<br>";
// DateTime 클래스의 생성자에 '@+타임스탬프'를 지정하면
// 그 타임스탬프의 DateTime 객체를 생성할 수 있음
$date = new DateTime("@$timestamp");
// 타임스탬프를 지정하여 생성된 DateTime 객체는 현재의 시스템 시간대가
// 무시되어 UTC가 되기 때문에 시간대를 Asia/Seoul로 변경함
$date->setTimezone(new DateTimeZone('Asia/Seoul'));
echo "날짜로 변환 확인: " . date('Y/m/d H:i:s', $timestamp) . "</p>";
?>
</div>
</body>
</html>
|
2018년 5월 13일 일요일
[PHP예제] 066 날짜를 타임스탬프로 변환하고 싶을 때
피드 구독하기:
댓글 (Atom)

댓글 없음:
댓글 쓰기