如何在php中输出周间隔(interval-> w)?

时间:2019-03-26 12:52:51

标签: php wordpress advanced-custom-fields

我正在尝试计算从小狗的生日到现在有多长时间了。

我从acf(高级自定义字段)输入中获取生日值。

我也设法输出了几个月和几天,但是,我正在努力弄清楚如何显示星期。

function puppies_how_old() {
    // getting acf input value
    $date = get_field('birthday_of_puppies');

    // converting
    $birthday = new DateTime($date);
    $interval = $birthday->diff(new DateTime);
    $interval_m = $interval->m;
    $interval_d = $interval->d;
    $interval_w = /* output of weeks interval */

    // outputting e.g 1 month(s) and 3 week(s) and 5 day(s)
    return $interval_m.' month(s) and '.$interval_w.' week(s) and '.$interval_d.' day(s);
}

// generating shortcode
add_shortcode('puppies_how_old', 'puppies_how_old');

编辑: 得到它的工作。感谢您指出相关问题!

我的结束代码:

function puppies_how_old() {
    // getting acf input value
    $date = get_field('birthday_of_puppies');

    // converting
    $birthday = new DateTime($date);
    $interval = $birthday->diff(new DateTime);
    $interval_m = $interval->m;
    $interval_d = $interval->d % 7;;
    $interval_w = floor($interval->d/7);

    // outputting e.g 1 month(s) and 3 week(s) and 5 day(s)
    return $interval_m.' month(s) and '.$interval_w.' week(s) and '.$interval_d.' day(s)';
}

// generating shortcode
add_shortcode('puppies_how_old', 'puppies_how_old');

0 个答案:

没有答案