我怎样才能得到“年份”? CodeIgniter 中 timespan( ) 的输出的一部分?

发布于 2024-07-07 08:51:07 字数 132 浏览 4 评论 0原文

我有一个“出生日期”字段,并尝试使用 timespan() 方法来获取年龄(以年为单位)。 但返回“28 年、2 个月、2 周、3 天、15 小时、16 分钟”。

知道如何才能获得“28 年”部分吗?

I have a "Date of Birth" field, and trying to use the timespan() method to get the age in years. But returns "28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes".

Any idea how I can just get the "28 Years" portion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2024-07-14 08:51:07

我建议你使用 PHP 的 strftime() 函数。 而不是使用 CI 的 timespan()。

echo strftime('%Y', 1226239392);

I suggest you use PHP's strftime() function. Instead of using the CI's timespan().

echo strftime('%Y', 1226239392);

孤独岁月 2024-07-14 08:51:07

有很多方法可以使用 $date 中的字符串来执行此操作,如下所示:

$date = '28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes';

This will give you "28 Years"

$yearsPart = substr($date, 0, strpos($date, 'Years') + 5);

So will this:

$parts = split(', ', $date);
$yearsPart = $parts[0];

There are many ways to do this, with the string in $date, like so:

$date = '28 Years, 2 Months, 2 Weeks, 3 Days, 15 Hours, 16 Minutes';

This will give you "28 Years"

$yearsPart = substr($date, 0, strpos($date, 'Years') + 5);

So will this:

$parts = split(', ', $date);
$yearsPart = $parts[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文