CakePHP:显示年龄的时间助手

发布于 2024-09-29 09:07:15 字数 190 浏览 8 评论 0原文

我有一个 MySQL DATETIME 字段,用于保存用户的出生日期。我想使用该日期来显示用户的年龄(以年为单位)。

Time core helper 有没有办法做到这一点?

另外,为什么当您向 Cake 中的表单添加 DATETIME 字段时,它不允许您选择小于 1990 年的日期?

谢谢,

琼斯

I have a MySQL DATETIME field that holds the date of birth of a user. I want to use that date to display the age the user in years.

Is there a method of the Time core helper to do this?

Also, why when you add a DATETIME field to a form in Cake does it not let you select a date less than 1990?

Thanks,

Jonesy

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

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

发布评论

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

评论(5

清旖 2024-10-06 09:07:15

该函数名为 timeAgoInWords ,

<?php echo $this->Time->timeAgoInWords(
      '1975-12-29 13:40', 
      array(
            'end'=>'+150 years'
      )
);?>

end 参数的意思是用单词显示之前的时间,直到日期小于 150 年(至少我还没听说过 150yo man :))))。

目前的结果是: 出生: 34 岁零 9 个月零 4 周之前

但还要按照 Jason 的建议检查 API 和 CookBook

the function is called timeAgoInWords

<?php echo $this->Time->timeAgoInWords(
      '1975-12-29 13:40', 
      array(
            'end'=>'+150 years'
      )
);?>

The end parameter mean to display the time ago with words until the date is less than 150 years (at least I haven't heard of 150yo man :)))).

The current result is: born: before 34 years, 9 months, 4 weeks

But also check API and CookBook as Jason suggest

淡笑忘祈一世凡恋 2024-10-06 09:07:15

添加新答案,因为代码会显示得更好。我将如何做到这一点:

在您的模型中创建 虚拟字段 像这样。

var $virtualFields = array(
   'age' => "YEAR(NOW())-YEAR(YourModelName.datetimeField)"
);

然后将其用作模型中的普通场。 :)

Adding new answer because the code will be shown better. How I would do that:

in your model create virtual field like this.

var $virtualFields = array(
   'age' => "YEAR(NOW())-YEAR(YourModelName.datetimeField)"
);

Then use it as normal field in the model. :)

陌伤ぢ 2024-10-06 09:07:15

timeAgoinWords() 可能会执行您想要的第一部分操作。查看 Book

这与自动表单字段有关。您可以使用选项调整年份范围。查看图书

timeAgoinWords() may do what you want for the first part. Check out the Book

This has to do with the automagic form fields. You can adjust the year range with options. Check out the Book.

向日葵 2024-10-06 09:07:15

您可以使用标准 PHP 函数执行计算 - 查看日期部分。我不会告诉你如何做,因为你自己做会学到很多东西。

我会说你需要小心才能得到准确的结果。 365 天并不总是一年,一个月的天数也不同。

您可以在 Cake 表单助手中配置日期选择范围。

You can perform the calculation using standard PHP functions - look in the date section. I'm not going to tell you how to do it because you'll learn a huge amount by doing it yourself.

I will say that you need to be careful to get an accurate result. 365 days is not always a year and a month has a variable number of days.

You can configure the date selection range in the Cake form helper.

七度光 2024-10-06 09:07:15

我总是使用这种经过测试的方法:

/*
 * @return int age (0 if both timestamps are equal or empty, -1 on invalid dates)
 * 2009-03-12 ms
 */
function age($start = null, $end = null, $accuracy = 0) {
    $age = 0;
    if (empty($start) && empty($end) || $start == $end) {
        return 0;
    }

    if (empty($start)) {
        list($yearS, $monthS, $dayS) = explode('-', date(FORMAT_DB_DATE));
    } else {
        $startDate = $this->fromString($start);
        $yearS = date('Y', $startDate);
        $monthS = date('m', $startDate);
        $dayS = date('d', $startDate);
        if (!checkdate($monthS, $dayS, $yearS)) {
            return -1;
        }
    }
    if (empty($end)) {
        list($yearE, $monthE, $dayE) = explode('-', date(FORMAT_DB_DATE));
    } else {
        $endDate = $this->fromString($end);
        $yearE = date('Y', $endDate);
        $monthE = date('m', $endDate);
        $dayE = date('d', $endDate);
        if (!checkdate($monthE, $dayE, $yearE)) {
            return -1;
        }
    }

    $n_tag = $dayE;
    $n_monat = $monthE;
    $n_jahr = $yearE;
    $g_tag = $dayS;
    $g_monat = $monthS;
    $g_jahr = $yearS;
    $g_date = mktime(0,0,0,$g_tag,$g_monat,$g_jahr);

    if (($n_monat>$g_monat)||(($n_monat == $g_monat)&&($n_tag>$g_tag))||(($n_monat == $g_monat)&&($n_tag==$g_tag))) {
        $age = $n_jahr-$g_jahr; // is correct if one already had his birthday this year
    } else {
        $age = $n_jahr-$g_jahr-1; // is correct if one didnt have his birthday yet in this year
    }
    return $age;
}

它可能可以被重构
但它适用于闰年和各种东西。所以我从来没有打扰过...

它使用一个你没有的常量

:define('FORMAT_DB_DATE', 'Ymd');

i always use this tested method:

/*
 * @return int age (0 if both timestamps are equal or empty, -1 on invalid dates)
 * 2009-03-12 ms
 */
function age($start = null, $end = null, $accuracy = 0) {
    $age = 0;
    if (empty($start) && empty($end) || $start == $end) {
        return 0;
    }

    if (empty($start)) {
        list($yearS, $monthS, $dayS) = explode('-', date(FORMAT_DB_DATE));
    } else {
        $startDate = $this->fromString($start);
        $yearS = date('Y', $startDate);
        $monthS = date('m', $startDate);
        $dayS = date('d', $startDate);
        if (!checkdate($monthS, $dayS, $yearS)) {
            return -1;
        }
    }
    if (empty($end)) {
        list($yearE, $monthE, $dayE) = explode('-', date(FORMAT_DB_DATE));
    } else {
        $endDate = $this->fromString($end);
        $yearE = date('Y', $endDate);
        $monthE = date('m', $endDate);
        $dayE = date('d', $endDate);
        if (!checkdate($monthE, $dayE, $yearE)) {
            return -1;
        }
    }

    $n_tag = $dayE;
    $n_monat = $monthE;
    $n_jahr = $yearE;
    $g_tag = $dayS;
    $g_monat = $monthS;
    $g_jahr = $yearS;
    $g_date = mktime(0,0,0,$g_tag,$g_monat,$g_jahr);

    if (($n_monat>$g_monat)||(($n_monat == $g_monat)&&($n_tag>$g_tag))||(($n_monat == $g_monat)&&($n_tag==$g_tag))) {
        $age = $n_jahr-$g_jahr; // is correct if one already had his birthday this year
    } else {
        $age = $n_jahr-$g_jahr-1; // is correct if one didnt have his birthday yet in this year
    }
    return $age;
}

it can probably be refactured
but it works with leap years and all kinds of stuff. so i never bothered...

it uses a constant you dont have

its just: define('FORMAT_DB_DATE', 'Y-m-d');

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