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

ube*_*sch 3 php usability jquery date-formatting

有没有一种简单的方法可以将 unix 时间戳、MySQL 时间戳、MySQL 日期时间(或任何其他标准日期和时间格式)转换为以下形式的字符串:

  • 今天下午 6:00
  • 明天中午 12:30
  • 周三下午 4:00
  • 下周五上午 11:00

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

Luk*_*son 5

据我所知,没有用于此的本机函数。我已经创建了一个函数(的开始)来执行您想要的操作。

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 );
}
Run Code Online (Sandbox Code Playgroud)

希望有帮助。