트위터나 페이스북을 보면 날짜 시간이 아니라
1분 전, 1시간 전 이런 표시가 되어 있는데
소스 구현해봤습니다.
UTF-8, PHP 최신버전에서 활용하세요~
파일도 첨부합니다.
[code]
<?php
include './TextDate.php';
$now = new DateTime;
$textData = new TextDate();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
echo $textData->print_(new DateTime("now")), "<br>\n";
echo $textData->print_(new DateTime("-1 day")), "<br>\n";
echo $textData->print_(new DateTime("-1 week")), "<br>\n";
echo $textData->print_(new DateTime("-1 week -20 days")), "<br>\n";
?>
</body>
</html>
[/code]
[code]
<?php
/**
* 날짜 문자
*
* 1분 전, 1시간 전, 1일 전 표시
*
* @author onhae
* @copyright onhae
* @license GPL
* @link http://www.onhae.com
* @since Version 1.0
*/
class TextDate {
/**
* 현재 날짜
* @var DateTime
*/
protected $now = null;
/**
* 비교 날짜
* @var DateTime
*/
protected $datetime = null;
/**
* 이전 표시 문자
* @var string
*/
protected $agoText = '약 %s 전';
/**
* 현재 표시 문자
* @var string
*/
protected $nowText = '현재';
/**
* 기본 형식
* @var string
*/
protected $defaultFormat = 'Y-m-d H:i:s';
/**
* 형식 배열 array('y' => '년', 'm' => '월', 'd' => '일', 'h' => '시간', 'i' => '분', 's' => '초')
* @var array
*/
protected $formatArray = array('y' => '년', 'm' => '개월', 'd' => '일', 'h' => '시간', 'i' => '분', 's' => '초');
/**
* 생성자
* @param DateTime $now 현재 날짜
* @param DateTime $datetime 비교 날짜
*/
public function __construct(DateTime $now = null, DateTime $datetime = null) {
if ($now === null) {
$this->now = new DateTime;
}
if ($datetime === null) {
$this->datetime = new DateTime;
}
}
/**
* 현재 날짜 설정
* @param DateTime $now
*/
function setNow(DateTime $now) {
$this->now = $now;
}
/**
* 비교 날짜 설정
* @param DateTime $datetime
*/
function setDatetime(DateTime $datetime) {
$this->datetime = $datetime;
}
/**
* 이전 표시 문자 설정
* @param string $agoText
*/
function setAgoText($agoText) {
$this->agoText = $agoText;
}
/**
* 현재 표시 문자
* @param string $nowText
*/
function setNowText($nowText) {
$this->nowText = $nowText;
}
/**
* 기본 형식 설정
* @param string $defaultFormat
*/
function setDefaultFormat($defaultFormat) {
$this->defaultFormat = $defaultFormat;
}
/**
* 형식 배열 설정 array('y' => '년', 'm' => '월', 'd' => '일', 'h' => '시간', 'i' => '분', 's' => '초')
* @param array $formatArray
*/
function setFormatArray(array $formatArray) {
$this->formatArray = $formatArray;
}
/**
* 출력
* @param DateTime $datetime 비교 할 날짜
* @return type
*/
public function print_(DateTime $datetime = null) {
if ($datetime === null) {
$datetime = $this->datetime;
}
/* @var $diff DateInterval */
$diff = $this->now->diff($datetime);
foreach ($this->formatArray as $key => $text) {
if ($diff->{$key} === 0) {
continue;
}
$result = $diff->{$key} . $text;
if ($diff->invert === 1) {
$result = sprintf($this->agoText, $result);
}
break;
}
if (isset($result) === false && $datetime->getTimestamp() === $this->now->getTimestamp()) {
$result = $this->nowText;
} elseif (isset($result) === false) {
$result = $datetime->format($this->defaultFormat);
}
return $result;
}
}
[/code]
소스가 잘 안 보이는 거 같아요...
첨부파일 다운받으세요..
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7830 | 9년 전 | 385 | ||
| 7829 |
|
9년 전 | 564 | |
| 7828 | 9년 전 | 495 | ||
| 7827 | 9년 전 | 381 | ||
| 7826 | 9년 전 | 387 | ||
| 7825 | 9년 전 | 440 | ||
| 7824 | 9년 전 | 424 | ||
| 7823 | 9년 전 | 329 | ||
| 7822 | 9년 전 | 324 | ||
| 7821 | 9년 전 | 269 | ||
| 7820 | 9년 전 | 324 | ||
| 7819 |
|
10년 전 | 729 | |
| 7818 | 10년 전 | 347 | ||
| 7817 | 10년 전 | 466 | ||
| 7816 | 10년 전 | 370 | ||
| 7815 | 10년 전 | 575 | ||
| 7814 | 10년 전 | 401 | ||
| 7813 | 10년 전 | 341 | ||
| 7812 | 10년 전 | 349 | ||
| 7811 | 10년 전 | 368 | ||
| 7810 | 10년 전 | 513 | ||
| 7809 | 10년 전 | 442 | ||
| 7808 | 10년 전 | 312 | ||
| 7807 | 10년 전 | 368 | ||
| 7806 |
프로그래머7
|
10년 전 | 1311 | |
| 7805 | 10년 전 | 1236 | ||
| 7804 |
zahir1312
|
10년 전 | 748 | |
| 7803 |
|
10년 전 | 1350 | |
| 7802 | 10년 전 | 414 | ||
| 7801 | 10년 전 | 836 | ||
| 7800 | 10년 전 | 1069 | ||
| 7799 | 10년 전 | 509 | ||
| 7798 | 10년 전 | 469 | ||
| 7797 | 10년 전 | 466 | ||
| 7796 | 10년 전 | 305 | ||
| 7795 | 10년 전 | 460 | ||
| 7794 | 10년 전 | 490 | ||
| 7793 | 10년 전 | 1012 | ||
| 7792 | 10년 전 | 417 | ||
| 7791 | 10년 전 | 502 | ||
| 7790 | 10년 전 | 467 | ||
| 7789 |
fbastore
|
10년 전 | 1409 | |
| 7788 | 10년 전 | 500 | ||
| 7787 | 10년 전 | 361 | ||
| 7786 | 10년 전 | 518 | ||
| 7785 | 10년 전 | 535 | ||
| 7784 | 10년 전 | 604 | ||
| 7783 | 10년 전 | 409 | ||
| 7782 | 10년 전 | 454 | ||
| 7781 | 10년 전 | 867 | ||
| 7780 | 10년 전 | 789 | ||
| 7779 | 10년 전 | 756 | ||
| 7778 | 10년 전 | 323 | ||
| 7777 | 10년 전 | 414 | ||
| 7776 | 10년 전 | 416 | ||
| 7775 | 10년 전 | 356 | ||
| 7774 | 10년 전 | 605 | ||
| 7773 | 10년 전 | 342 | ||
| 7772 | 10년 전 | 678 | ||
| 7771 | 10년 전 | 336 | ||
| 7770 | 10년 전 | 619 | ||
| 7769 | 10년 전 | 338 | ||
| 7768 | 10년 전 | 557 | ||
| 7767 | 10년 전 | 1125 | ||
| 7766 | 10년 전 | 455 | ||
| 7765 | 10년 전 | 486 | ||
| 7764 |
잘살아보자
|
10년 전 | 336 | |
| 7763 |
|
10년 전 | 1404 | |
| 7762 |
Tosea
|
10년 전 | 1026 | |
| 7761 | 10년 전 | 615 | ||
| 7760 |
잘살아보자
|
10년 전 | 627 | |
| 7759 |
잘살아보자
|
10년 전 | 435 | |
| 7758 |
잘살아보자
|
10년 전 | 542 | |
| 7757 | 10년 전 | 1193 | ||
| 7756 |
ITBANK
|
10년 전 | 1225 | |
| 7755 | 10년 전 | 1906 | ||
| 7754 | 10년 전 | 1014 | ||
| 7753 | 10년 전 | 846 | ||
| 7752 | 10년 전 | 1354 | ||
| 7751 |
잘살아보자
|
10년 전 | 478 | |
| 7750 |
잘살아보자
|
10년 전 | 445 | |
| 7749 |
잘살아보자
|
10년 전 | 447 | |
| 7748 |
잘살아보자
|
10년 전 | 436 | |
| 7747 |
잘살아보자
|
10년 전 | 532 | |
| 7746 |
잘살아보자
|
10년 전 | 647 | |
| 7745 |
잘살아보자
|
10년 전 | 886 | |
| 7744 |
잘살아보자
|
10년 전 | 394 | |
| 7743 | 10년 전 | 920 | ||
| 7742 |
starbros
|
10년 전 | 801 | |
| 7741 |
잘살아보자
|
10년 전 | 618 | |
| 7740 |
잘살아보자
|
10년 전 | 488 | |
| 7739 |
잘살아보자
|
10년 전 | 444 | |
| 7738 |
잘살아보자
|
10년 전 | 499 | |
| 7737 |
잘살아보자
|
10년 전 | 459 | |
| 7736 |
잘살아보자
|
10년 전 | 477 | |
| 7735 |
잘살아보자
|
10년 전 | 809 | |
| 7734 |
잘살아보자
|
10년 전 | 406 | |
| 7733 |
잘살아보자
|
10년 전 | 510 | |
| 7732 |
잘살아보자
|
10년 전 | 664 | |
| 7731 |
잘살아보자
|
10년 전 | 586 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기