人类可读的,当前时间敏感的PHP日期和时间格式 [英] Human-readable, current time sensitive date and time formatting in PHP

查看:161
本文介绍了人类可读的,当前时间敏感的PHP日期和时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以轻松地将Unix时间戳,MySQL时间戳,MySQL日期时间(或任何其他标准日期和时间格式)转换为以下形式的字符串:

Is there a painless way to convert unix timestamps, MySQL timestamps, MySQL datetimes (or any other standard date and time format) into strings in the following form:

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

我不确定该怎么称呼-我猜是对话式的,当前时间敏感的日期格式吗?

I'm not sure what to call these - I guess conversational-style, current time sensitive date formats?

推荐答案

据我所知,没有为此功能提供的本机功能.我已经创建了一个函数(开始)来完成您想要的事情.

As best I can tell, there is no native function for this. I have created (the start of) a function to do what you are wanting.

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 );
}

希望有帮助.

这篇关于人类可读的,当前时间敏感的PHP日期和时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆