PHP 5 Date and Time


PHP date() 함수는 날짜 또는 시간을 포멧하는데 사용됩니다. .


PHP Date() Function

PHP date() 함수는 날짜와 시간을 더 잘 읽을 수 있게 timestamp 를 포멧합니다.

Syntax

date(format,timestamp)

Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

 timestamp 는 특정 이벤트가 발생한 날짜/시간을 나타내는 문 자 시퀀스이다.



간단한 Date 얻기

date() 함수의 필수인 포멧(format) 파라메타는 날짜(또는 시간)를 어떤 형식으로 나타내는지 명시한다. 

일반적으로 날짜에 사용되는 일부 문자는 다음과 같습니다.:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
  • l - Represents the day of the week

"/", ".", 또는 "-" 과 같은 다른 문자들도 추가적인 서식으로 문자 사이에 삽입될 수 있습니다.

아래의 예는 세가지 다른 방법으로 오늘 날짜를 포맷한다.:

Example

<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>

Run example »


PHP Tip - 자동 저작권 연도(Automatic Copyright Year)

웹사이트에 저작권 년도를 자동으로 업데이트하는데 date() 함수를 사용합니다.:

Example

&copy; 2010-<?php echo date("Y")?>

Run example »


간단한 시간 얻기

시간에 일반적으로 사용되는 일부 문자들은 다음과 같다:

  • h - 0 으로 시작하는 12-hour format 시간 (01 to 12)
  • i - 0 으로 시작하는 Minutes  (00 to 59)
  • s - 0 으로 시작하는 Seconds (00 to 59)
  • a - 소문자 오전(Ante meridiem)과 오후(Post meridiem) (am or pm)

아래의 예는 지정된 형식으로 현재 시간을 출력한다.:

Example

<?php
echo "The time is " . date("h:i:sa");
?>

Run example »

 PHP date() 함수는 서버의 현재 날짜/시간을 반환합니다.



시간대(Time Zone) 얻기

코드로부터 반환 받은 시간이 정확한 시간이 아닐 경우, 서버가 다른 나라 또는 다른 시간대로 설정되어 있기 때문일 수 있습니다.

따라서, 특정 위치에 따른 올바른 시간이 필요한 경우에는, 사용할 시간대를 설정할 수 있습니다.

아래의 예는 "America/New_York" 시간대로, 그리고 특정 형식으로 현재 시간을 출력한다.:

Example

<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>

Run example »


PHP mktime()로 Date 생성

date() 함수의 선택적인(optional) timestamp 파라메타는 timestamp를 지정한다.  timestamp를 지정하지 않으면, 현재 날짜와 시간이 사용된다 (위의 예와 같이).

mktime() 함수는 날짜의 Unix timestamp 를 반환한다.  Unix timestamp 는 Unix Epoch (January 1 1970 00:00:00 GMT) 와 지정한 시간 사이의 초(seconds)의 수를 포함한다. 

Syntax

mktime(hour,minute,second,month,day,year)

아래의 예제에서는 mktime() 함수에서 여러 매개변수로 날짜와 시간을 생성한다. :

Example

<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Run example »


PHP strtotime()으로 String 으로부터 날짜 생성

PHP strtotime() 함수는 사람이 인식할 수 있는 문자열을 Unix time으로 변환하는데 사용된다.

Syntax

strtotime(time,now)

아래의 예는  strtotime() 함수로 날짜외 시간을 생성한다.:

Example

<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Run example »

PHP 는 문자열을 날짜로 변환하는데 있어서 매우 영리하다, 그래서 당신은 다양한 값을 넣을 수 있다.:

Example

<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";

$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";

$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>

Run example »

그러나, strtotime() 은 완벽하지는 않으므로, 넣을 문자열을 확인하는 것을 기억하시오.


More Date Examples

아래의 예는 6개의 다음 토요일의 날짜를 출력합니다. :

Example

<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks",$startdate);

while ($startdate < $enddate) {
  echo date("M d", $startdate),"<br>";
  $startdate = strtotime("+1 week", $startdate);
}
?>

Run example »

아래의 예는 7월 4일까지의 일 수를 출력합니다.:

Example

<?php
$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>

Run example »


Complete PHP Date Reference

For a complete reference of all date functions, go to our complete PHP Date Reference.

The reference contains a brief description, and examples of use, for each function!