트위터나 페이스북을 보면 날짜 시간이 아니라
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년 전 | 442 | ||
| 7829 |
|
9년 전 | 625 | |
| 7828 | 10년 전 | 554 | ||
| 7827 | 10년 전 | 451 | ||
| 7826 | 10년 전 | 454 | ||
| 7825 | 10년 전 | 494 | ||
| 7824 | 10년 전 | 457 | ||
| 7823 | 10년 전 | 404 | ||
| 7822 | 10년 전 | 377 | ||
| 7821 | 10년 전 | 324 | ||
| 7820 | 10년 전 | 359 | ||
| 7819 |
|
10년 전 | 756 | |
| 7818 | 10년 전 | 405 | ||
| 7817 | 10년 전 | 541 | ||
| 7816 | 10년 전 | 423 | ||
| 7815 | 10년 전 | 622 | ||
| 7814 | 10년 전 | 452 | ||
| 7813 | 10년 전 | 402 | ||
| 7812 | 10년 전 | 414 | ||
| 7811 | 10년 전 | 404 | ||
| 7810 | 10년 전 | 584 | ||
| 7809 | 10년 전 | 517 | ||
| 7808 | 10년 전 | 396 | ||
| 7807 | 10년 전 | 423 | ||
| 7806 |
프로그래머7
|
10년 전 | 1349 | |
| 7805 | 10년 전 | 1291 | ||
| 7804 |
zahir1312
|
10년 전 | 790 | |
| 7803 |
|
10년 전 | 1387 | |
| 7802 | 10년 전 | 476 | ||
| 7801 | 10년 전 | 872 | ||
| 7800 | 10년 전 | 1093 | ||
| 7799 | 10년 전 | 572 | ||
| 7798 | 10년 전 | 527 | ||
| 7797 | 10년 전 | 531 | ||
| 7796 | 10년 전 | 372 | ||
| 7795 | 10년 전 | 525 | ||
| 7794 | 10년 전 | 573 | ||
| 7793 | 10년 전 | 1069 | ||
| 7792 | 10년 전 | 494 | ||
| 7791 | 10년 전 | 573 | ||
| 7790 | 10년 전 | 522 | ||
| 7789 |
fbastore
|
10년 전 | 1461 | |
| 7788 | 10년 전 | 564 | ||
| 7787 | 10년 전 | 419 | ||
| 7786 | 10년 전 | 613 | ||
| 7785 | 10년 전 | 605 | ||
| 7784 | 10년 전 | 657 | ||
| 7783 | 10년 전 | 467 | ||
| 7782 | 10년 전 | 507 | ||
| 7781 | 10년 전 | 912 | ||
| 7780 | 10년 전 | 835 | ||
| 7779 | 10년 전 | 782 | ||
| 7778 | 10년 전 | 373 | ||
| 7777 | 10년 전 | 479 | ||
| 7776 | 10년 전 | 471 | ||
| 7775 | 10년 전 | 418 | ||
| 7774 | 10년 전 | 638 | ||
| 7773 | 10년 전 | 395 | ||
| 7772 | 10년 전 | 752 | ||
| 7771 | 10년 전 | 404 | ||
| 7770 | 10년 전 | 656 | ||
| 7769 | 10년 전 | 405 | ||
| 7768 | 10년 전 | 627 | ||
| 7767 | 10년 전 | 1188 | ||
| 7766 | 10년 전 | 505 | ||
| 7765 | 10년 전 | 553 | ||
| 7764 |
잘살아보자
|
10년 전 | 413 | |
| 7763 |
|
10년 전 | 1476 | |
| 7762 |
Tosea
|
10년 전 | 1074 | |
| 7761 | 10년 전 | 666 | ||
| 7760 |
잘살아보자
|
10년 전 | 704 | |
| 7759 |
잘살아보자
|
10년 전 | 537 | |
| 7758 |
잘살아보자
|
10년 전 | 611 | |
| 7757 | 10년 전 | 1253 | ||
| 7756 |
ITBANK
|
10년 전 | 1268 | |
| 7755 | 10년 전 | 1936 | ||
| 7754 | 10년 전 | 1072 | ||
| 7753 | 10년 전 | 897 | ||
| 7752 | 10년 전 | 1409 | ||
| 7751 |
잘살아보자
|
10년 전 | 553 | |
| 7750 |
잘살아보자
|
10년 전 | 491 | |
| 7749 |
잘살아보자
|
10년 전 | 503 | |
| 7748 |
잘살아보자
|
10년 전 | 519 | |
| 7747 |
잘살아보자
|
10년 전 | 605 | |
| 7746 |
잘살아보자
|
10년 전 | 689 | |
| 7745 |
잘살아보자
|
10년 전 | 930 | |
| 7744 |
잘살아보자
|
10년 전 | 429 | |
| 7743 | 10년 전 | 959 | ||
| 7742 |
starbros
|
10년 전 | 852 | |
| 7741 |
잘살아보자
|
10년 전 | 684 | |
| 7740 |
잘살아보자
|
10년 전 | 558 | |
| 7739 |
잘살아보자
|
10년 전 | 475 | |
| 7738 |
잘살아보자
|
10년 전 | 550 | |
| 7737 |
잘살아보자
|
10년 전 | 522 | |
| 7736 |
잘살아보자
|
10년 전 | 541 | |
| 7735 |
잘살아보자
|
10년 전 | 868 | |
| 7734 |
잘살아보자
|
10년 전 | 439 | |
| 7733 |
잘살아보자
|
10년 전 | 554 | |
| 7732 |
잘살아보자
|
10년 전 | 712 | |
| 7731 |
잘살아보자
|
10년 전 | 630 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기