PHP中可读的、当前时间敏感的日期和时间格式
是否有一种无痛的方式将unix时间戳、MySQL时间戳、MySQL日期时间(或任何其他标准日期和时间格式)转换为以下形式的字符串:PHP中可读的、当前时间敏感的日期和时间格式,php,jquery,usability,date-formatting,Php,Jquery,Usability,Date Formatting,是否有一种无痛的方式将unix时间戳、MySQL时间戳、MySQL日期时间(或任何其他标准日期和时间格式)转换为以下形式的字符串: 今天下午6:00 明天下午12:30 星期三下午4:00 下周五上午11:00 我不知道该怎么称呼这些——我想是对话式的,当前对时间敏感的日期格式?应该会有用 示例: echo date('d, h:i:sa', strtotime($date_orig)); PHP日期函数有点混乱,因为它们有太多不同的方法,甚至新类都是在旧类的基础上构建的。对于这种类型的格
- 今天下午6:00
- 明天下午12:30
- 星期三下午4:00
- 下周五上午11:00
echo date('d, h:i:sa', strtotime($date_orig));
PHP日期函数有点混乱,因为它们有太多不同的方法,甚至新类都是在旧类的基础上构建的。对于这种类型的格式,我称之为人性化格式,您必须编写自己的函数来实现它 对于转换,您可以使用前面提到的strotime(),但是如果您处理的是大纪元时间并且需要utc GMT时间,这里有一些函数。strotime()会将历元时间转换为本地服务器时间。。。这是我在我的项目中不想要的东西
/**
* 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('Y-m-d H:i:s');
}
你应该阅读和的文档 将UNIX时间戳转换为您的格式的示例:
$time = time(); // UNIX timestamp for current time
echo strftime("%A, %l:%M %P"); // "Thursday, 12:41 pm"
要获取MySQL datetime值,假设它从数据库中显示为“2010-07-15 12:42:34”,请尝试以下操作:
$time = "2010-07-15 12:42:34";
echo strftime("%A, %l:%M %P"); // "Thursday, 12:42 pm"
现在,为了打印单词“today”而不是day名称,您必须执行一些额外的逻辑来检查日期是否为today:
$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);
}
据我所知,这个函数没有本机函数。我已经创建了一个函数来做你想做的事情
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 );
}
函数timeToString($inTimestamp){
$now=时间();
if(绝对值($inTimestamp-$now)$now)
返回‘明天’。$t;
返回‘昨天’。$t;
}
如果($inTimestamp-$now)>0){
如果($inTimestamp-$now<604800)#在未来7天内
返回日期('l,g:ia',$inTimestamp);
如果($inTimestamp-$now<1209600)#在接下来的14天内,但在接下来的7天之后
返回“下一个”。日期('l,g:ia',$inTimestamp);
}否则{
如果($now-$inTimestamp<604800)#在过去7天内
返回“最后”。日期('l,g:ia',$inTimestamp);
}
#改天
返回日期('l jS F,g:ia',$inTimestamp);
}
希望对您有所帮助。如果您要从数据库中提取此类数据
$time = "2010-07-15 12:42:34";
那就这样做吧
$this->db->select('DATE_FORMAT(date, "%b %D %Y")AS date');
请在此处查找信息,以便以任何方式以人形显示您的数据
上面的代码是codeigniter格式的,但是您可以将其转换为MYSQL的SELECT语句
$query = "SELECT `title`,`post`, DATE_FORMAT(date, "%a, %d %b %Y %T") AS date FROM `posts` LIMIT 0, 8 ";
您需要更改%a字母以满足您的需要。谢谢Jesse,我希望有人能将我链接到一个PHP库,该库已经为您描述的内容编写好了(希望还有更多内容)。如果我必须自己写,你的帮助让我开始了。@ubermensch你写了那个库吗?@looneydoodle抱歉,伙计,从来没有:)
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';
}