用于时间和日期的PHP开关语句

问题描述 投票:0回答:3

我有一个非常复杂的交货情况,有一个转换语句,它检查一周中的某天,每天的截止时间为16:00,并且回声是星期一,星期二和星期三是今天的交货日+ 1天(如果已过帐)截止日期应与今天的日期+ 2相呼应,由于周末不提供服务,因此周四和周五的交付日期完全不同

<?php
    $today = date("D");
        switch($today){
            case "Mon":
            if(mktime(16, 0, 0) <= time()) {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 2 days')) . "</strong><p>";
                } else {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
                }
                break;
            case "Tue":
                if(mktime(16, 0, 0) <= time()) {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 2 days')). "</strong><p>";
                } else {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
                }
                break;
            case "Wed":
                if(mktime(16, 0, 0) <= time()) {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 2 days')). "</strong><p>";
                } else {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
                }
                break;
            case "Thu":
            if(mktime(16, 0, 0) <= time()) {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 4 days')). "</strong><p>";
                } else {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 1 days')). "</strong><p>";
                }
                break;
            case "Fri":
                if(mktime(16, 0, 0) <= time()) {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 5 days')). "</strong><p>";
                } else {
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 4 days')). "</strong><p>";
                }
                break;
            case "Sat":
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 4 days')). "</strong><p>";
                break;
            case "Sun":
                    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. ' + 3 days')). "</strong><p>";
                break;
            default:
                echo "No information available for that day.";
                break;
        }
        ?>

当然,任何人都应该知道采用更优雅的方式来实现相同的结果,它们必须是实现这一目标的更优雅的方式。此代码有效,但可能会更好,更短]

php switch-statement
3个回答
1
投票

一个快速的尝试,就像我在评论中提到的,删除重复的代码,这既是用于回显的字符串,又是能够组织切换的函数,因此任何具有共同值的日子都将由同一代码处理。 ..

$today = date("D");
switch($today){
    case "Mon":
    case "Tue":
    case "Wed":
        $days = (mktime(16, 0, 0) <= time())? 2 : 1;
        break;
    case "Thu":
        $days = (mktime(16, 0, 0) <= time())? 4 : 1;
        break;
    case "Fri":
        $days = (mktime(16, 0, 0) <= time())? 5 : 4;
        break;
    case "Sat":
        $days = 4;
        break;
    case "Sun":
        $days = 3;;
        break;
    default:
        break;
}
if ( isset($days) ) {
    echo "<p>For Delivery on <strong> " . date('D jS', strtotime($Date. " + {$days} days")). "</strong><p>";
}
else    {
    echo "No information available for that day.";
}

2
投票

注意,您可以堆叠具有相同功能的case匹配项:

switch ($today) {
    case 'Mon':
    case 'Tue':
    case 'Wed':
        // code here
        break;
}

但是最好只制作一个包含当日所需值的小数组,然后通过将日期名称用作数组索引来查找今天的值:

$leadtimes = [
    'Mon' => [1, 2], // first value is for before 16:00, second is for after
    'Tue' => [1, 2],
    'Wed' => [1, 2],
    'Thu' => [1, 4],
    'Fri' => [4, 5],
    'Sat' => [4, 4],
    'Sun' => [3, 3],
];

然后,用于计算提前期的代码仅一行:

$leadtime = $leadtimes[date('D')][date('H') < 16 ? 0 : 1];

然后像以前一样将其插入输出中:

echo
    "<p>For Delivery on <strong> " .
    date('D jS', strtotime("$Date + $leadtime days")) .
    "</strong><p>";

现在,当您要更改提前期时,无需编写任何代码,只需调整数组中的值即可。

而且因为我做过任何代码高尔夫已经有一段时间了,所以整件事都在一行上:

echo date('D jS',strtotime('+'.['Mon'=>[1,2],'Tue'=>[1,2],'Wed'=>[1,2],'Thu'=>[1,4],'Fri'=>[4,5],'Sat'=>[4,4],'Sun'=>[3,3]][date('D')][date('H')<16].'day'));

0
投票

怎么样:

$today = date("D");
$cutoff = mktime(16, 0, 0) <= time();

$offset = $cutoff ? 2 : 1;
$offset = $cutoff and $today === 'Thu' ? 4 : 1;
$offset = $cutoff and $today === 'Fri' ? 5 : 4;
$offset = $today === 'Sat' ? 4 : $offset;
$offset = $today === 'Sun' ? 3 : $offset;

echo "<p>For Delivery on <strong> " . date('D jS', strtotime("$Date $offset days")) . "</strong><p>";

此外,我建议使用Carbon处理日期。

© www.soinside.com 2019 - 2024. All rights reserved.