php date 转换 string_php将时间差转换为字符串提示
通过传入数据库中存储的文章发表时的UNIX时间戳,来转化为例如 几分钟前,几小时前,几天前 这样的提示。如微博
这看起来更加人性化,好吧,上代码
class timeAgo
{
static $timeagoObject;
private $rustle;
private $unit;
private function __construct()
{
}
private function __clone(){ }
public static function getObject()
{
if(! (self::$timeagoObject instanceof self) )
self::$timeagoObject = new timeAgo();
return self::$timeagoObject;
}
private function count_int($unix_C) // main function
{
if(! (isset($unix_C) || is_numeric($unix_C)) )
return 'don\'t find parameter';
$d = time()-$unix_C ; // $d - unix time difference value
$d_int =(int)floor($d/60) ; // minimum unit -- minutes unix/60
$this->unit = 0 ; // is minutes,hour or day?
if($d_int < 60){ // minutes in one hour 3600
$this->rustle = $d_int;
$this->unit = 1;
}
else if($d_int < 720){ //hour in one day 3600*12
$this->rustle = floor($d_int/60);
$this->unit = 2 ;
}
else if($d_int < 7200){ //day in ten days 3600*12*10
$this->rustle = floor($d_int/720);
$this->unit = 3 ;
}
else{
$this->rustle = $d ;
$this->unit = 4 ;
}
}
public function piece_str($C)
{
$this->count_int($C);
$u = '';
switch( $this->unit )
{
case 1:
$u = 'minute';
break;
case 2:
$u = 'hour';
break;
case 3:
$u = 'day';
break;
case 4:
$u = '';
break;
case 0:
return 'sorry , get time is fail';
}
if($this->unit < 4)
{
if($this->rustle > 1)
return (string)$this->rustle.$u.'s ago';
else if($this->rustle == 1)
return (string)$this->rustle.$u.'ago';
else
return 'Just now';
}
}
/* example: $ago = timeAgo::getObject();
* echo $ago->piece_str($unix);
* // 2 days ago
*/
}
?>
php date 转换 string_php将时间差转换为字符串提示相关推荐
- php把时间戳转换成字符串,php中将时间差转换为字符串提示的实现代码
如微博 这看起来更加人性化,好吧,上代码 class timeAgo { static $timeagoObject; private $rustle; private $unit; private ...
- JavaScript 技术篇-js自动转换类型,自动转换为字符串,js避免自动转换的坑
自动转换类型 当 JavaScript 尝试操作一个 "错误" 的数据类型时,会自动转换为 "正确" 的数据类型. 而往往这种隐式的转化可能给程序带来很多莫名奇 ...
- java字符如何向float转换_java – 将float转换为字符串分数表示
最简单的方法可能是使用反复试验. public static String toFraction(double d, int factor) { StringBuilder sb = new Stri ...
- 在Java中将字符串转换为日期,将日期转换为字符串
Sometimes we have to Convert String to Date in java program or convert Date to String in a different ...
- vue时间转换为字符串
方法1: var testDate = new Date(); // toString() 把 Date 对象转换为字符串. var dateobj_toString = testDate.toStr ...
- 将字符串转换为日期,将日期转换成指定格式的字符串
/** * 将字符串转换为日期 * @param str * @return */ public static Date toDate(String str){ DateForm ...
- python整数转换字符串_使用Python中的str()函数将整数值转换为字符串
python整数转换字符串 Given an integer value and we have to convert the value to the string using str() func ...
- scala 字符串转换数组_如何在Scala中将字节数组转换为字符串?
scala 字符串转换数组 Byte Array in Scala is an array of elements of a byte type. String in Scala is a colle ...
- php 把数组转换成字符串_php怎么将数组转换为字符串
php将数组转换为字符串的方法:可以使用implode()函数分割数组,该函数会返回由数组元素组成的字符串.还可以利用循环遍历数组元素并将其拼接成字符串. php将数组转换为字符串的方法 方法1:利用 ...
最新文章
- multi-mono-sf:自监督多帧单目场景流估计
- FPGA的设计艺术(3)静态时序分析
- 更改android AVD模拟器创建路径位置的方法
- 干 MySQL 两千万数据的大表优化解决过程,三种厉害的解决方案
- SLinkList(静态链表)
- (4opencv)OpenCV PR 成功的收获和感悟
- Jeecg社区wiki在开放,终于可以在线看文档啦!!!
- limcon.v3.60_uk 英国\
- 分析 AlphaGo 算法原理的本质
- 李铁被传下课之际,梅西却要七拿金球奖了?这波预测没毛病
- sql条件查询表数据条数
- 一个学习C语言的好网站,推荐给大家
- 随机搜索 /爬山法/模拟退火/遗传算法
- 信息数据管理思维导图(Xmind)
- 国内各大短视频平台去水印接口,新年限时免费使用。
- Python3 解决编码问题: UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in position
- 语义分割【论文解读】新思路:几何感知蒸馏 CVPR-2019
- CSS中的块级元素、行内元素和行内块元素
- 【装机必备】Win软件卸载工具
- CDN - 阿里云CDN全站加速,说说动态加速那些事
热门文章
- 第八届全国功能基因组学高峰论坛-微生物分会场 完美落幕!
- NAR:浙大陈云/马忠华团队揭示病原真菌组蛋白H3K27甲基化识别新机制
- Nature Plants:河南大学王学路团队揭示大豆与根瘤菌匹配性的进化及其分子机制...
- QIIME 2用户文档. 9数据导入Importing data(2019.7)
- QIIME1.9.1-2使用Docker运行QIIME
- pandas使用query函数基于判断条件获得dataframe中满足条件的数据行(row)的索引列表(index of rows matching conditions in dataframe)
- R语言使用ggplot2包和maps包可视化美国地图、使用北美犯罪率数据为不同区域的地图渲染(颜色深浅区分犯罪率高低、US map colored by violent crime rates)
- java interface class_Java中常量定义在interface和class的区别(转)
- 德布鲁因图和OLC组装基因组
- Biopython-Chapter3.生物序列对象