将之前的时间转换为其日期时间

时间:2019-02-13 23:23:03

标签: php

我想知道是否可以将SELECT si.*, sn.first_name, sn.last_name, so.songname, so.date, so.category FROM singer si INNER JOIN singerNote sn ON sn.singer# = si.singer# INNER JOIN songs so ON so.singerlabel = si.singerlabel AND NOT EXISTS ( SELECT 1 FROM songs so1 WHERE so1.singerlabel = si.singerlabel AND so1.date > so.date ) 转换为time ago

例如date time5 years, 9 months, 1 week, 5 days ago

我一直在使用此2013-05-01来将function转换为date,但如何进行相反操作!

time ago

用法:

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

1 个答案:

答案 0 :(得分:1)

为什么不能这样做:

//uses and modifies today's date 2019-02-13
$date = (new DateTime)->modify('-5 years -9 months -1 week -5 days')->format('Y-m-d');

echo $date."\n";

//takes the modified data and does the reverse
$date = (new DateTime($date))->modify('+5 years +9 months +1 week +5 days')->format('Y-m-d');

echo $date."\n";

输出

2013-05-01
2019-02-13 //today

Sandbox

只要您创建该字符串以符合相对日期格式,

http://php.net/manual/en/datetime.formats.relative.php

我不明白为什么您不能将其重新插入datetime类。

相关问题