如何根据PHP中的数量复制日,周和月的单词

时间:2017-10-06 01:28:26

标签: php date datetime format

经过很长一段时间没有编码并且仍然是相对较新的php我创建了以下代码块来计算狗和小狗的年龄。在不同的生命阶段,年龄的测量方式不同,只有几天,几周,几天,几个月和几周,最后是几年和几个月。

我目前有两个问题需要在以下代码中解决。

  1. 取决于年龄的结果,结果可能是" 2年1个月"我需要代码足够聪明才能将输出更改为" 2年1个月"在这种情况下,月份是单数的没有#39;我需要做几年,几个月,几周和几天的事情。我知道这可以通过相当多的代码膨胀来完成,但我确信有一种干净的方法可以做到。

  2. 仍然无法弄清楚如何检查传递给该功能的日期是在过去还是将来(例如2周内一窝小狗),以便代码可以更通用

  3. 我只是一个老狗,自从用kb测量计算机内存并且你是一名“高级程序员”以来,他一直在编码。如果你可以导航和编程ISAM数据集。

    function CalcAgeDOB($DOBdate)
    {
        date_default_timezone_set("America/Chicago");
        $datetime1 = date_create($DOBdate);
        $datetime2 = date_create();
        $DifferenceFormatDays = "%a";
    
        $interval = date_diff($datetime1, $datetime2);
    
        $TotalDays = (($interval->format($DifferenceFormatDays)));
    
        Switch ($TotalDays) {
            case ($TotalDays < 0):
                $ReturnText = "We are still waiting on arrival";
                break;
            case ($TotalDays == 0):
                $ReturnText = "We were born today";
                break;
            case (($TotalDays >= 1) and ($TotalDays < 31)):
                $ReturnText = $interval->format("%a Days");
                break;
            case (($TotalDays > 30) and ($TotalDays < 91)):
                $WholeWeeks = (int)(($TotalDays) / 7);
                $RemainDays = $TotalDays % 7;
                $ReturnText = "$WholeWeeks Weeks $RemainDays Days";
                break;
            case (($TotalDays > 90) and ($TotalDays < 366)):
                $MonthDays = $interval->format('%m,%d');
                $MonthDaysArray = explode(",", $MonthDays);
                $WholeMonths = $MonthDaysArray[0];
                $WholeWeeks = (int)($MonthDaysArray[1] / 7);
                $ReturnText = "$WholeMonths Months $WholeWeeks Weeks";
                break;
            case ($TotalDays > 365):
                $ReturnText = $interval->format('%y Years %m Months');
                break;
            default:
                $ReturnText = "Error";
                break;
        }
    
        return $ReturnText;    
    }
    
    echo CalcAgeDOB("02/08/2017");
    

    ----------------编辑10/06/2017 -------------------------

    在实施@ Cy-Rossignol的更改后更新了代码块,这解决了问题1.我仍需要理清传递给函数的日期是过去还是将来。当传递$ quantity的零时,多个函数也从建议更新为返回空字符串。根据建议将转换后的控制结构转换为if / elseif。

    <?php
    
    function pluralize($quantity, $singular, $plural, $format = '%d %s') 
    {
        if (empty($quantity)) {
            return sprintf(""); 
        }
    
        if ($quantity === 1) {
            return sprintf($format, $quantity, $singular); 
        }
    
        return sprintf($format, $quantity, $plural); 
    }
    
    function CalcAgeDOB($DOBdate)
    {
        date_default_timezone_set("America/Chicago");
        $datetime1 = date_create($DOBdate);
        $datetime2 = date_create();
        $DifferenceFormatDays = "%a";
    
        $interval = date_diff($datetime1, $datetime2);
    
        $TotalDays = ((int)($interval->format($DifferenceFormatDays)));
    
        if ($TotalDays < 0) {
                $ReturnText = "We are still waiting on arrival";
        } elseif (empty($TotalDays)) {
                $ReturnText = "We were born today";
        } elseif (($TotalDays >= 1) and ($TotalDays < 31)) {
                $ReturnText = pluralize(($TotalDays % 7), 'Day', 'Days');
        } elseif  (($TotalDays > 30) and ($TotalDays < 91)) {
                $ReturnText = pluralize(((int)($TotalDays / 7)), 'Week', 'Weeks') . ' ' . pluralize(($TotalDays % 7), 'Day', 'Days');
        } elseif (($TotalDays > 90) and ($TotalDays < 366)) {
                $MonthDays = $interval->format('%m,%d');
                $MonthDaysArray = explode(",", $MonthDays);
                $WholeMonths =(int)($MonthDaysArray[0]);
                $WholeWeeks = (int)($MonthDaysArray[1] / 7);
                $ReturnText = pluralize($WholeMonths, 'Month', 'Months') . ' ' . pluralize($WholeWeeks, 'Week', 'Weeks');
        } elseif ($TotalDays > 365) {
                $YearsMonths = $interval->format('%y,%m');
                $YearsMonthsArray = explode(",", $YearsMonths);
                $WholeYears =  (int)($YearsMonthsArray[0]);
                $WholeMonths = (int)($YearsMonthsArray[1]);
                $ReturnText = pluralize($WholeYears, 'Year', 'Years') . ' ' . pluralize($WholeMonths, 'Month', 'Months');
        } else {
                $ReturnText = "Error";            
        }
    
        return $ReturnText;
    
    }
    
    
    
    echo CalcAgeDOB("7/3/2016")
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

对于英语,我们可以编写一个简单的函数,根据提供的数量选择合适的单词形式:

function pluralize($quantity, $singular, $plural, $format = '%d %s') 
{
    if ($quantity === 1) {
        return sprintf($format, $quantity, $singular); 
    }

    return sprintf($format, $quantity, $plural); 
}

以下是一些示例用法:

pluralize(5, 'child', 'children');              // "5 children"
pluralize(1, 'foot', 'feet');                   // "1 foot"
pluralize(0, 'day', 'days');                    // "0 days"
pluralize(3, 'person', 'people', '%2$s: %1$d'); // "people: 3"

最后一个示例显示了sprintf()允许的自定义格式。以下是我们如何使用问题中示例中的函数:

function CalcAgeDOB($DOBDate) 
{
    ...
    switch ($TotalDays) {
        ... 
        case (($TotalDays > 30) and ($TotalDays < 91)):
            $ReturnText = pluralize((int)($TotalDays / 7), 'Week', 'Weeks') 
                . ' ' . pluralize($TotalDays % 7, 'Day', 'Days'); 
            );
            break;
...

使用日期时,请考虑使用Carbon库,它将PHP的日期和时间功能包含在一个出色的API中:

$dob = Carbon::parse('2017-10-01 00:00:00'); 

$dob->diffInDays(Carbon::now());    // "5"
Carbon::now()->diffForHumans($dob); // "5 days ago"

我们可以将此库与上面的pluralize()函数结合使用。