1

I have a leave system where user select dates from and to dates let say 2018-02-26 - 2018-03-03 that is 6 days in total. On our portal it shows all users that is on leave. supposing that this user applied for 6 days leave, How am I going to display this user for 6 days on our portal?below is my code it works well but is show only for the current date.

$currentDate = date('Y-m-d');

$lf = Leave::where('leave_from', $currentDate)
                       ->where('status', 'approved')
                       ->get();

I am using eloquent. thanks

pal3
  • 241
  • 1
  • 2
  • 13
  • [Possible duplicate of "PHP: Return all dates between two dates in an array "](https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – h2ooooooo Feb 26 '18 at 07:02

6 Answers6

3

// Specify the start date. This date can be any English textual format  
$date_from = "2018-02-03";   
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp  
  
// Specify the end date. This date can be any English textual format  
$date_to = "2018-02-10";  
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp  
  
// Loop from the start date to end date and output all dates inbetween  
for ($i=$date_from; $i<=$date_to; $i+=86400) {  
    echo date("Y-m-d", $i).'<br />';  
} 
Mohini
  • 268
  • 3
  • 15
  • Thanks @mohini.. but on the next day how am I going to query from db since the date stored is only date_from and date_to? – pal3 Feb 26 '18 at 07:17
2

You can use datetime function in PHP.

$first_date = new DateTime($currentDate);

$last_date = new DateTime($if);

$difference = $first_date->diff($last_date);

echo $difference->d.' days; 

if you want month and year use $difference->m and $difference->y.And if you want accurate days even dates are negative use below one.

$result = $first_date->diff($last_date)->format("%r%a");

source:/ datetime

lalithkumar
  • 3,480
  • 4
  • 24
  • 40
  • did you check this? @zluj – lalithkumar Feb 26 '18 at 07:26
  • @lalitkumar thanks but how about on the next day what query should I use? – pal3 Feb 26 '18 at 07:54
  • every day the current date is getting updated know..what you mean next day? – lalithkumar Feb 26 '18 at 08:42
  • on my table I have this fields `leave_from` and `leave_end` let say leave from has a value of `2018-02-26` and leave end is `2018-03-03` so that is 6 days right?I want to display this on my page for 6 days. – pal3 Feb 26 '18 at 09:04
  • you can fetch the leave_from and leave_end from DB and give it as $currentdate and $if then you will get the $result..You can show the $result wherever you want. – lalithkumar Feb 26 '18 at 09:08
1

You can use date_diff() function:

$interval = date_diff($currentDate, $lf);
echo $interval->format('%d');

more on: http://php.net/manual/en/function.date-diff.php; https://www.w3schools.com/php/func_date_date_diff.asp

Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
1

You can use DATEDIFF to get difference directly from query.

Select abs(DATEDIFF(`date_to`,`date_from`)) as diff from leaves;

In laravel you can write it as

$lf = Leave::where('status', 'approved')
      ->select(DB::raw("abs(DATEDIFF(`date_to`,`date_from`)) as diff"),"*")
      ->get();

Be sure to use where('leave_from', $currentDate), I think it's not necessary.

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

You can use DateTime function of PHP in your case

$currentDate = date('Y-m-d');
$currentDate = new DateTime($currentDate);
$leaveDate = new DateTime($leaveFrom);
$difference = $currentDate->diff($leaveDate);

You will get result like

DateInterval Object ( [y] => 0 [m] => 0 [d] => 5 [h] => 0 [i] => 0 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 5 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
PPL
  • 6,357
  • 1
  • 11
  • 30
0
                      $many_days=0;
                $date_from = "2018-02-26";      
                  $date_to = "2018-03-03";    
                for ($i=strtotime($date_from);   $i<=strtotime($date_to);  
                     $i+=86400) {  

                       $many_days++;
                         } 

              echo  "leaves $many_days";