[英]Human-readable, current time sensitive date and time formatting in PHP
是否可以通过以下方式轻松地将unix时间戳,MySQL时间戳,MySQL日期时间(或任何其他标准日期和时间格式)转换为字符串:
我不确定该怎么称呼-我猜是会话式的当前时间敏感日期格式吗?
尽我所能告诉我们,没有本机功能。 我已经创建了一个函数(开始)来执行您想要的操作。
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 );
}
希望能有所帮助。
将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"
现在,为了打印单词“ 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);
}
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('Y-m-d H:i:s');
}
如果您要从数据库中提取此类数据
$time = "2010-07-15 12:42:34";
然后做这个
$this->db->select('DATE_FORMAT(date, "%b %D %Y")AS date');
在此处查找信息,以您想要的任何形式以人类形式显示数据
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';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.