当前位置:网站首页>php convert timestamp to just, minutes ago, hours ago, days ago format
php convert timestamp to just, minutes ago, hours ago, days ago format
2022-08-08 23:11:00 【Do you still believe in light?】
The following are several ways of php implementation, you can choose by yourself
//Convert the timestamp to minutes or hours or days agofunction wordTime($time) {$time = (int) substr($time, 0, 10);$int = time() - $time;$str = '';if ($int <= 30){$str = sprintf('Just now', $int);}elseif($int < 60){$str = sprintf('%d seconds ago', $int);}elseif ($int < 3600){$str = sprintf('%d minutes ago', floor($int / 60));}elseif ($int < 86400){$str = sprintf('%d hours ago', floor($int / 3600));}elseif ($int < 2592000){$str = sprintf('%d days ago', floor($int / 86400));}else{$str = date('Y-m-d H:i:s', $time);}return $str;}
function mdate($time = NULL) {$text = '';$time = $time === NULL || $time > time() ? time() : intval($time);$t = time() - $time; // time difference (seconds)$y = date('Y', $time)-date('Y', time());//Whether New Year's Eveswitch($t){case $t == 0:$text = 'Just now';break;case $t < 60:$text = $t . 'seconds ago'; // within a minutebreak;case $t < 60 * 60:$text = floor($t / 60) . 'minutes ago'; //within an hourbreak;case $t < 60 * 60 * 24:$text = floor($t / (60 * 60)) . 'hour ago'; // within a daybreak;case $t < 60 * 60 * 24 * 3:$text = floor($time/(60*60*24)) ==1 ?'Yesterday' .date('H:i', $time) : 'The day before yesterday' .date('H:i', $time) ; //yesterday and the day before yesterdaybreak;case $t < 60 * 60 * 24 * 30:$text = date('m month d day H:i', $time); //within a monthbreak;case $t < 60 * 60 * 24 * 365&&$y==0:$text = date('m month d day', $time); //within one yearbreak;default:$text = date('Y year m month d day', $time); //a year agobreak;}return $text;}
I hope this helps!!!!
边栏推荐
猜你喜欢
随机推荐
(2022牛客多校五)B-Watches(二分)
微信小程序错误 undefined Expecting ‘STRING‘,‘NUMBER‘,‘NULL‘,‘TRUE‘,‘FALSE‘,‘{‘,‘[‘, got ]解决方案
Kubernetes与OpenStack
Qt入门(五)——文件操作、热键和鼠标的读取(txt窗口的实现)
(2022牛客多校四)A-Task Computing (排序+动态规划)
JSDay2- 长度最小的子数组
(Codeforce 757)E. Bash Plays with Functions(积性函数)
WeChat applet wx:for loop output example
(nowcoder22529C)dinner(容斥原理+排列组合)
(2022牛客多校四)H-Wall Builder II(思维)
Hand-written prometheus exporter-01-Gauge (dashboard)
【CUDA】版本自由切换
最详树莓派4B装机流程及ifconfig不到wlan0的解决办法
深拷贝与浅拷贝
(2022牛客多校四)N-Particle Arts(思维)
tp5用cache缓存,存储手机短信验证码
WeChat applet develops some function usage methods
wps表格怎么调整表格大小?wps表格调整表格大小的方法
[PP-YOLOv2] Training a custom dataset
设计分享|基于单片机的P0口驱动LED闪烁