PHP将日期转换为“空白”天/小时/秒前 [英] PHP Convert date to "blank" days/hours/seconds ago

查看:96
本文介绍了PHP将日期转换为“空白”天/小时/秒前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将date()转换为年/月/周/天/小时/分钟/秒。我如何做?

I want to convert date() to "" years/months/weeks/days/hours/minutes/seconds ago. How do I do that?

我正在使用 date('M j,Y')日期进入数据库。有没有更有效的方式来保存上述我想要生产的日期?

I'm currently using date('M j, Y') when saving the date into a database. Is there a more effective way to save the date for what I want to produce above?

(我的网站不存在,所以我可以做任何我需要的数据库和代码,如果需要)

(My site is not live yet so I can do whatever I need in the database and code if need be)

推荐答案

您应该始终以MySQL的日期时间格式(YYYY-MM-DD)保存日期。这使您可以轻松利用MySQL的内置日期功能。以任何其他格式存储意味着(可能很多)更多的工作,当你想要做更多,然后只是显示这些值。

You should always save your dates in MySQL's datetime format (YYYY-MM-DD). This allows you to easily take advantage of MySQL's built in date functionality. Storing it in any other format means (potentially a lot) more work for you when you want to do more then just display those values.

完成你想要做的使用PHP,您将 DateTime() (根据此答案):

To accomplish what you want to do with PHP you would DateTime() (based on this answer):

$datetime1 = new DateTime($firstDate);
$datetime2 = new DateTime($secondDate);
$interval  = $datetime1->diff($datetime2);
if ($interval->days <= 7)
{
    $elapsed   = $interval->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
    $elapsed   = str_replace(array('0 years,', ' 0 months,', ' 0 days,',  ' 0 hours,', ' 0 minutes,'), '', $elapsed);
    $elapsed   = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',  ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
    echo $elapsed;
}
else 
{
    echo $firstDate;
}

$datetime1 = new DateTime($firstDate);
$datetime2 = new DateTime($secondDate);

这些行创建 DateTime()他们各自的日期。

These lines create DateTime() objects with their respective dates.

$interval  = $datetime1->diff($datetime2);

此行从第一行减去第二个日期,并将差值作为 DateInterval()对象

This lines subtracts the second date from the first and returns the difference as a DateInterval() object.

if ($interval->days > 7)

此行检查以查看两个日期之间已经有七天以上的时间。如果是,则执行第一个代码块。如果没有,则打印出第一个日期。

This line checks to see seven or more days have elapsed between the two dates. If so the first code block is executed. If not, the first date is printed out.

$elapsed   = $interval->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
$elapsed   = str_replace(array('0 years,', ' 0 months,', ' 0 days,',  ' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed   = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',  ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
echo $elapsed;

这个代码块只是在两个日期之间取得日期差异(a DateInterval() 对象),并以格式格式化你要求第二行删除任何没有值(即0个月)的时间段,并将其从字符串中删除。第三行需要一个值(即1个月)的任何期间,并在结束时修剪不必要的(即1个月变为1个月)。

This code block just takes the date difference between the two dates (a DateInterval() object) and formats it in the format you requested. The second lines removes any periods of time that have no values (i.e. 0 months) and removes them from the string. The third line takes any periods with one value (i.e. 1 months) and trims off the unnecessary 's' at the end (i.e. 1 months becomes 1 month).

这篇关于PHP将日期转换为“空白”天/小时/秒前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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