How to finding the number of days between two days of the week?

↘锁芯ラ 提交于 2019-12-08 14:10:54

问题


How to find the number of days between two days not dates using PHP?

I know how to get the number of days between two dates, but my input values are day names (date-ignorant).

Inputs/Outputs:

Wednesday and Saturday returns 3

Sunday and Wednesday returns 3


回答1:


Your task doesn't seem to require date functions at all. A simple lookup array will suffice.

  1. Subtract the starting day's integer value from the ending day's integer.
  2. If the difference would be zero or less, add 7 to always return the correct, positive day count.

Code: (Demo)

function daysUntil($start, $end) {
    $lookup = [
        'Sunday' => 0,
        'Monday' => 1,
        'Tuesday' => 2,
        'Wednesday' => 3,
        'Thursday' => 4,
        'Friday' => 5,
        'Saturday' => 6
    ];
    $days = $lookup[$end] - $lookup[$start] + ($lookup[$end] <= $lookup[$start] ? 7 : 0);
    return "{$days} days from {$start} to {$end}\n";
}

echo daysUntil('Wednesday', 'Saturday');  // Thursday, Friday, Saturday
echo daysUntil('Monday', 'Friday');       // Tuesday, Wednesday, Thursday, Friday
echo daysUntil('Thursday', 'Thursday');   // [assumed next week]
echo daysUntil('Friday', 'Monday');       // Saturday, Sunday, Monday
echo daysUntil('Saturday', 'Sunday');     // Sunday
echo daysUntil('Sunday', 'Saturday');     // Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
echo daysUntil('Sunday', 'Wednesday');    // Monday, Tuesday, Wednesday

Output:

3 days from Wednesday to Saturday
4 days from Monday to Friday
7 days from Thursday to Thursday
3 days from Friday to Monday
1 days from Saturday to Sunday
6 days from Sunday to Saturday
3 days from Sunday to Wednesday

Or you can replace the lookup array with 4 function calls and achieve the same outcome: (Demo)

function daysUntil($start, $end) {
    $days = date('w', strtotime($end)) - date('w', strtotime($start));
    $days += $days < 1 ? 7 : 0;
    return "{$days} days from {$start} to {$end}\n";
}



回答2:


Use the PHP date_diff() function (docs).

$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);

$interval = date_diff($datetime1, $datetime2);

echo $interval->format('%d'); // For days

Per the clarification, you could create arbitrary Saturdays and Wednesdays to calculate it:

$datetime1 = date_create(date('Y-m-d',strtotime('wednesday')));
$datetime2 = date_create(date('Y-m-d',strtotime('saturday')));

$interval = date_diff($datetime1, $datetime2);

echo $interval->format('%d'); // For days

Would return "3", but depending when you ran it.




回答3:


You can write the name of the day for parsing in a new DateTime class:

<?php
  $datetime1 = new DateTime('Sunday');
  $datetime2 = new DateTime('Wednesday');
  $interval = $datetime1->diff($datetime2);
  echo $interval->format('%R%a days');
  ?>

Also below the one line version:

echo (new DateTime('Sunday'))->diff(new DateTime('Wednesday'))->format('%a days');



回答4:


public function numOfDaysBetween($start,$end)
{
$days=[0=>'Saturday',1=>'Sunday',2=>'Monday',3=>'Tuesday',4=>'Wednesday',5=>'Thursday',6=>'Friday'];
$index_start=array_search($start, $days);
    $index_end=array_search($end, $days);
    if($index_start==$index_end)
    {
        return 7;
    }else if($index_start<$index_end)
    {
        return $index_end-$index_start;
    }else
    {
        return 7-($index_start-$index_end);
    }

}




回答5:


You can use php CARBON library for manipulating time and date. See Documentation



来源:https://stackoverflow.com/questions/47160521/how-to-finding-the-number-of-days-between-two-days-of-the-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!