PHP中人类可读,当前时间敏感的日期和时间格式

是否有一种无痛的方法将unix时间戳,MySQL时间戳,MySQL日期时间(或任何其他标准日期和时间格式)转换为以下forms的字符串:

  • 今天下午6点
  • 明天下午12:30
  • 星期三,下午4点
  • 下个星期五,上午11:00

我不知道该怎么称呼这些 – 我猜对话风格,当前时间敏感的日期格式?

我能说的最好,没有原生function。 我已经创建了(一个开始)一个函数来做你想要的事情。

function timeToString( $inTimestamp ) { $now = time(); if( abs( $inTimestamp-$now )<86400 ) { $t = date('g:ia',$inTimestamp); if( date('zY',$now)==date('zY',$inTimestamp) ) return 'Today, '.$t; if( $inTimestamp>$now ) return 'Tomorrow, '.$t; return 'Yesterday, '.$t; } if( ( $inTimestamp-$now )>0 ) { if( $inTimestamp-$now < 604800 ) # Within the next 7 days return date( 'l, g:ia' , $inTimestamp ); if( $inTimestamp-$now < 1209600 ) # Within the next 14, but after the next 7 days return 'Next '.date( 'l, g:ia' , $inTimestamp ); } else { if( $now-$inTimestamp < 604800 ) # Within the last 7 days return 'Last '.date( 'l, g:ia' , $inTimestamp ); } # Some other day return date( 'l jS F, g:ia' , $inTimestamp ); } 

希望有所帮助。

你应该阅读strftimestrtotime的文档

将UNIX时间戳转换为格式的示例:

 $time = time(); // UNIX timestamp for current time echo strftime("%A, %l:%M %P"); // "Thursday, 12:41 pm" 

要获取MySQL日期时间值,假设它从数据库中出现为“2010-07-15 12:42:34”,请尝试以下操作:

 $time = "2010-07-15 12:42:34"; echo strftime("%A, %l:%M %P"); // "Thursday, 12:42 pm" 

现在,为了打印“今天”而不是日期名称,您将需要做一些额外的逻辑来检查日期是否为今天:

 $time = "2010-07-15 12:42:34"; $today = strftime("%Y-%m-%d"); // compare if $time strftime's to the same date as $today if(strftime("%Y-%m-%d", $time) == $today) { echo strftime("Today, %l:%M %P", $time); } else { echo strftime("%A, %l:%M %P", $time); } 

strtotime shoule对此有用。

例:

 echo date('d, h:i:sa', strtotime($date_orig)); 

PHP日期函数有点混乱,因为它们有很多不同的方式,甚至新的类都建立在旧的类之上。 对于那种类型的格式化,我称之为人性化的格式化,你将不得不编写自己的函数来完成它。

对于转换,您可以使用所提到的strtotime(),但是如果您正在处理纪元时间并且需要utc GMT时间,则可以使用一些函数。 strtotime()会将纪元时间转换为本地服务器时间……这是我不想要的项目。

 /** * Converts a GMT date in UTC format (ie. 2010-05-05 12:00:00) * Into the GMT epoch equivilent * The reason why this doesnt work: strtotime("2010-05-05 12:00:00") * Is because strtotime() takes the servers timezone into account */ function utc2epoch($str_date) { $time = strtotime($str_date); //now subtract timezone from it return strtotime(date("Z")." sec", $time); } function epoch2utc($epochtime, $timezone="GMT") { $d=gmt2timezone($epochtime, $timezone); return $d->format('Ymd H:i:s'); } 

如果要从数据库中提取此类数据

 $time = "2010-07-15 12:42:34"; 

然后这样做

 $this->db->select('DATE_FORMAT(date, "%b %D %Y")AS date'); 

在这里查看信息,以人类的forms显示您想要的任何数据

http://www.w3schools.com/SQL/func_date_format.asp

上面的代码采用codeigniter格式,但您只需将其转换为MYSQL的SELECT语句即可

 $query = "SELECT `title`,`post`, DATE_FORMAT(date, "%a, %d %b %Y %T") AS date FROM `posts` LIMIT 0, 8 "; 

您需要更改%a字母以满足您的需求。

 you will get exact result:: output // 28 years 7 months 7 days function getAge(dateVal) { var birthday = new Date(dateVal.value), today = new Date(), ageInMilliseconds = new Date(today - birthday), years = ageInMilliseconds / (24 * 60 * 60 * 1000 * 365.25 ), months = 12 * (years % 1), days = Math.floor(30 * (months % 1)); return Math.floor(years) + ' years ' + Math.floor(months) + ' months ' + days + ' days'; }