0

I have this form with this input field

<input type="text" name="txt_datetimein" class="form-control datetime">
<input type="text" name="txt_datetimeout" class="form-control datetime">
<input type="text" name="txt_lenght" class="form-control">

I enter the first date and the second date and the length the length i precise the number of repetition

than i click next and i have all days without sunday

example if i put 5 in the length and datetimein 01-04-2017 8:00:00 and datetimeout is 01-04-2017 5:00:00

the output will be like that

    Date IN             Date Out           Day
01-04-2017 8:00:00  01-04-2017 5:00:00   Saturday
03-04-2017 8:00:00  03-04-2017 5:00:00   Monday
04-04-2017 8:00:00  04-04-2017 5:00:00   Tuesday
05-04-2017 8:00:00  05-04-2017 5:00:00   Wednesday
06-04-2017 8:00:00  06-04-2017 5:00:00   Thursday
07-04-2017 8:00:00  07-04-2017 5:00:00   Friday

this my code but it's print all day

<?php
    for($i=0;$i<=$lenght;$i++) {
        $date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));   
        $edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout))); 
        $is_sunday = date('l', strtotime($date)) == 'Sunday';             
        if ($is_sunday) {             
            $day = date('l', strtotime("+1+$i days",strtotime($datetimein)));
        }
        else {
            $day = date('l', strtotime("+$i days",strtotime($datetimein)));
        }  
    }
?>

How Can i solve my Problem ??!!

mohamad mohamad
  • 613
  • 9
  • 24
  • Because you are adding one day if its sunday, it doesnt display sunday? $day=date('l', strtotime("+1+$i days",strtotime($datetimein))); – Webbanditten Apr 20 '17 at 12:54
  • this line of code makes it so that you skip sunday`$day = date('l', strtotime("+1+$i days",strtotime($datetimein)));` the `+1` in `+1+$i` – Cr1xus Apr 20 '17 at 12:55
  • @Cr1xus i don't want to print sunday but my code print sunday – mohamad mohamad Apr 20 '17 at 12:56
  • Try http://stackoverflow.com/questions/12365461/day-difference-without-weekends – Rohan Kumar Apr 20 '17 at 13:03
  • you have described 5 as lenght and printed 7 dates , also datetime in and out is same date, where intime is bigger than out time !!! what you want to do, can yu tell us the senario ? so we can help better. If in and out dates are same why are you giving both ? you may have to enter starting date and how many dates you do need (the length). Correct ? – Alice Apr 20 '17 at 13:29
  • @Alice the date in and out are the same this true but the time is different time in is 8:00 am and out id 5:00 pm i enter a lenght for example 5 i want to print six day from $i=0,1,2,3,4,5 without sunday like the example and when the date is sunday i skip sunday and i print the next date with his real date – mohamad mohamad Apr 20 '17 at 13:32

4 Answers4

1

Use DateTime and all those objects. Simpler and cleaner :-)

<?php

$begin = new DateTime();
$end = clone $begin;
$end = $end->modify('+14 day'); 

$interval = new DateInterval('P1D');
$range = new DatePeriod($begin, $interval ,$end);

foreach($range as $date) {
    if ($date->format('N') !== 7) {
        echo $date->format('Y-m-d'), '<br>';
    }
}

Date format N is the day of week as a number where 7 === Sunday.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
1

try this below code

$datetimein = "01-04-2017 8:00:00";
    $datetimeout = "01-04-2017 5:00:00";
    $lenght = 20;
    for($i=0;$i<=$lenght;$i++) {
        $date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));   
        $edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout))); 
        $is_sunday = date('l', strtotime($date)); 
        if($is_sunday == "Sunday")
        {
            $i=$i+1;
        }
        $day = date('l', strtotime("$i days",strtotime($datetimein)));   

        echo $day."<br>";
    }
Shital Marakana
  • 2,817
  • 1
  • 9
  • 12
1

Here is you programe

$datetimein =   '01-04-2017 8:00:00';
$datetimeout=   '01-04-2017 5:00:00';
$lenght =   5;

$i=0;
$days   =   array();
$dt =   strtotime($datetimein);
while($i < $lenght){
    if(date('D',$dt)!='Sun'){
        $days[] =   date('Y-m-d D',$dt);        
        $i++;
    }
    $dt     =   $dt+24*3600;
}

print_r($days);

In this line $days[] = date('Y-m-d D',$dt); change the format or save both in and out time whatever you want. $days will have you expected dates.

Alice
  • 1,422
  • 1
  • 18
  • 30
0

It looks like you have mis-spelled the variable length. Check this.

<?php
$lenght = 5;
$in_temp = 0;
$out_temp = 0;
for($i=0;$i<=$lenght;$i++) {
    $in = strtotime($datetimein) + $in_temp;
    $out = strtotime($datetimeout) + $out_temp;
    $date = date('m/d/Y H:i:s', strtotime("+$i days", $in));   
    $edate = date('m/d/Y H:i:s', strtotime("+$i days", $out)); 
    $is_sunday = strtolower(date('l', strtotime($date))) == 'sunday';             
    if ($is_sunday) {
        $in_temp += 86400 ;  // Adding 1 day in seconds.
        $day = date('l', (strtotime($date)+$in_temp));
    }
    else {
        $day = date('l', (strtotime($date)));
    }
    echo $day."\n";
}
?>
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21