-1

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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
predactor
  • 772
  • 4
  • 11
  • 29

3 Answers3

3

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";
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • right i test the another answer but give wrong result – predactor Nov 08 '17 at 15:00
  • i aslo wrote another one yesterday but your's is simple thanks – predactor Nov 08 '17 at 15:00
  • happy to help. (be sure to always include your coding attempt with your questions -- this will avoid downvotes on your questions) – mickmackusa Nov 08 '17 at 15:02
  • ppls don't understand and fastly downvotes and spam it as duplicated – predactor Nov 08 '17 at 15:06
  • That is why it is so important to forge the very best question that you can. It is not too late to improve your question for the benefit of future SO readers. If you edit your question to include your failed coding attempt, I will consider upvoting it. – mickmackusa Nov 08 '17 at 20:45
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.

kchason
  • 2,836
  • 19
  • 25
  • I think I must be a little disgruntled since receiving a downvote on my correct answer a few hours ago. My apologies but I must downvote this answer because it doesn't provide the correct results. https://3v4l.org/BO44o – mickmackusa Jan 15 '18 at 22:06
-1

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');
Mihai Crăiță
  • 3,328
  • 3
  • 25
  • 37
  • My apologies, I must be a little disgruntled after copping a downvote on my correct answer a few hours ago. This answer does not provide the desired results. https://3v4l.org/lhVOu – mickmackusa Jan 15 '18 at 22:11