日期/时间从php转换为时间 [英] date / time convert to time since php

查看:105
本文介绍了日期/时间从php转换为时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中你的日期/时间是这样的格式

Hello in my database date / time are in this format

2010-06-01T18:20:25+0000

我想回应那个从那个日期开始的时间

I'd like to echo that out to time passed since that date / time

eg

4天3小时36分4秒

4 days 3 hours 36 minutes and 4 seconds

这是可能吗?

推荐答案

以下是我写的一个函数来做到这一点。可以自由使用它。

Below is a function I wrote to do this. Feel free to use it.

/**
 * Returns rough (in largest single unit) time elapsed between two times.
 * @param int $iTime0  Initial time, as time_t.
 * @param int $iTime1  Final time, as time_t. 0=use current time.
 * @return string Time elapsed, like "5 minutes" or "3 days" or "1 month".
 *              You might print "ago" after this return if $iTime1 is now.
 * @author Dan Kamins - dos at axonchisel dot net
 */
function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
{
    if ($iTime1 == 0) { $iTime1 = time(); }
    $iTimeElapsed = $iTime1 - $iTime0;

    if ($iTimeElapsed < (60)) {
        $iNum = intval($iTimeElapsed); $sUnit = "second";
    } else if ($iTimeElapsed < (60*60)) {
        $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
    } else if ($iTimeElapsed < (24*60*60)) {
        $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
    } else if ($iTimeElapsed < (30*24*60*60)) {
        $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
    } else if ($iTimeElapsed < (365*24*60*60)) {
        $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
    } else {
        $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
    }

    return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "");
}

要使用此功能,您需要先将时间转换为time_t格式(自历元以来的整数#秒)。这些PHP函数之一可能有助于: http://php.net/strptime http://php.net/strtotime

To use this func, you'd need to first convert your times to time_t format (integer #seconds since the "epoch"). Either of these PHP functions will probably help with that: http://php.net/strptime or http://php.net/strtotime.

这篇关于日期/时间从php转换为时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆