PHP CodeBase: 将时间显示为“刚刚”“n分钟/小时前”等

本文提供了一个将Unix时间戳转化为用户友好时间显示格式的PHP函数,适用于实时性显示需求,如微博、SNS应用。通过判断时间戳与当前时间的关系,函数能智能转换为'刚刚'、'几分钟前'、'几小时前'等形式。

在很多场合为了显示出信息的及时性,一般会将时间显示成“刚刚”,“5分钟前”,“3小时前”等,而不是直接将时间打印出来。比如微博,SNS类应用就最长用到这个功能。而一般存储在数据库中的时间格式为 Unix时间戳,所以这里记录一个将 Unix时间戳 转化为时间轴显示的PHP函数。

函数比较简单,直接看代码就很好懂了。

<?php

date_default_timezone_set('PRC');
$date = "1351836000";
echo tranTime($date);

function transfer_time($time)
{
    $rtime = date("m-d H:i",$time);
    $htime = date("H:i",$time);

    $time = time() - $time;

    if ($time < 60)
    {
        $str = '刚刚';
    }
    elseif ($time < 60 * 60)
    {
        $min = floor($time/60);
        $str = $min.'分钟前';
    }
    elseif ($time < 60 * 60 * 24)
    {
        $h = floor($time/(60*60));
        $str = $h.'小时前 '.$htime;
    }
    elseif ($time < 60 * 60 * 24 * 3)
    {
        $d = floor($time/(60*60*24));
        if($d==1)
            $str = '昨天 '.$rtime;
        else
            $str = '前天 '.$rtime;
    }
    else
    {
        $str = $rtime;
    }
    return $str;
}

?>

注意函数transfer_time()中的参数$time必须为Unix时间戳,如果不是请先用strtotime()将其转换成Unix时间戳

See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box. ************** Exception Text ************** System.ComponentModel.Win32Exception (0x80004005): The operation was canceled by the user at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at AutoUpdate.EHSHighEnergyNew.timer1_Tick(Object sender, EventArgs e) at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** Loaded Assemblies ************** mscorlib Assembly Version: 4.0.0.0 Win32 Version: 4.8.9310.0 built by: NET481REL1LAST_C CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll ---------------------------------------- CCMC OT Login Pad Assembly Version: 1.0.0.0 Win32 Version: 1.0.0.0 CodeBase: file:///C:/Users/shet/Desktop/CCMC%20OT%20Login%20Pad.exe ---------------------------------------- CrowdStrike.Sensor.ScriptControl Assembly Version: 7.23.19508.0 Win32 Version: 7.23.19508.0 CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/CrowdStrike.Sensor.ScriptControl/7.23.19508.0__02b60967b1f68187/CrowdStrike.Sensor.ScriptControl.dll ---------------------------------------- System.Windows.Forms Assembly Version: 4.0.0.0 Win32 Version: 4.8.9256.0 built by: NET481REL1LAST_B CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System Assembly Version: 4.0.0.0 Win32 Version: 4.8.9310.0 built by: NET481REL1LAST_C CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- System.Drawing Assembly Version: 4.0.0.0 Win32 Version: 4.8.9032.0 built by: NET481REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System.Configuration Assembly Version: 4.0.0.0 Win32 Version: 4.8.9032.0 built by: NET481REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------- System.Core Assembly Version: 4.0.0.0 Win32 Version: 4.8.9297.0 built by: NET481REL1LAST_C CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll ---------------------------------------- System.Xml Assembly Version: 4.0.0.0 Win32 Version: 4.8.9032.0 built by: NET481REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll ---------------------------------------- Accessibility Assembly Version: 4.0.0.0 Win32 Version: 4.8.9032.0 built by: NET481REL1 CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll ---------------------------------------- ************** JIT Debugging ************** To enable just-in-time (JIT) debugging, the .config file for this application or computer (machine.config) must have the jitDebugging value set in the system.windows.forms section. The application must also be compiled with debugging enabled. For example: <configuration> <system.windows.forms jitDebugging="true" /> </configuration> When JIT debugging is enabled, any unhandled exception will be sent to the JIT debugger registered on the computer rather than be handled by this dialog box. 报错信息
最新发布
09-06
<think> 我们正在处理用户提出的关于特定Win32异常的问题。用户遇到了一个System.ComponentModel.Win32Exception,错误代码为0x80004005,错误消息是"The operation was canceled by the user"。我们需要分析这个错误的原因并提供解决方案。 根据错误消息,很明显这个错误是由于用户取消了操作导致的。在Windows应用程序中,当用户通过点击取消按钮、关闭对话框或其他方式中断一个操作时,就可能抛出这个异常。 常见场景包括: - 用户点击了文件打开对话框或保存对话框的“取消”按钮 - 用户关闭了打印对话框 - 用户取消了网络操作 - 用户取消了系统权限提示(如UAC) 然而,这个错误也可能在非用户直接交互的上下文中出现,例如当程序调用某个系统API时,系统内部由于某些条件不满足而模拟了用户取消操作。 解决方案通常包括: 1. 在代码中正确处理取消操作,避免程序崩溃 2. 检查操作是否被用户取消,并给出友好的提示 3. 确保程序逻辑不会在用户取消后继续执行依赖该操作结果的代码 具体到编程实践,以下是一个示例: 假设我们有一个文件打开对话框: ```csharp OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) // 用户点击了“打开” { // 处理文件 } else // 用户点击了“取消” { // 这里可以什么都不做,或者提示用户操作已取消 // 注意:不要在这里抛出异常,因为取消是正常操作 } ``` 如果在没有用户界面的情况下出现此错误(例如在服务中),则可能是由于权限不足或其他系统策略导致操作被拒绝,系统将其视为“用户取消”。在这种情况下,需要检查: - 运行程序的账户权限 - 系统策略设置 - 是否有防病毒软件或安全软件拦截 另外,错误代码0x80004005是一个通用的COM错误代码,表示操作失败。结合错误消息,我们可以确定是用户取消导致的。 因此,总结解决方案: 1. 在用户界面操作中,捕获异常并优雅处理,或者通过条件判断避免异常发生(如上述对话框示例)。 2. 在非用户界面场景中,检查权限和系统策略,确保程序有足够的权限执行操作。 参考文献: [^1]: Microsoft Documentation on Win32Exception Class [^2]: Understanding HRESULT Values 相关问题: 1. Win32Exception错误0x80004005的其他常见原因是什么? 2. 如何在C#中捕获并处理Win32Exception? 3. 如何避免在用户
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值