1
<input type = "date" name = "myDate" onchange = ""> //this is input date 

When the user picks a date then it wants to change based on above date How to change based on given date please help me..!

<table class="table table-bordered" id="thbg">

<thead><td></td>

<th>Project</th>

<th>Activity</th>

<th>Bill Type</th>

<?php

$day = "1";
$month = "11";
$year = "2017";

$start_date = $day."-".$month."-".$year;
$start_time = strtotime($start_date);

$end_time = strtotime("+1 week", $start_time);

for($i=$start_time; $i<$end_time; $i+=86400)
{
  print  '<th align="center"> '. date("m-d-Y  l", $i). '</th>';
}
?>
<th>Hours</th>      
</thead>
</table>

And the output is also getting wrong dates, e.g. I am getting two Sundays at a time

this is my wrong output

Can anyone suggest a solution?

3
  • 1
    Just a comment - your HTML is technically invalid. You should have a <tr> around all your <td>s and <th>s - i.e. table cells must be within a table row, which can itself be within a <thead> or <tbody>, and those must be within a <table> element. Commented Nov 30, 2017 at 11:21
  • Yaa i changed my code with <table>, but this is not my solution Commented Nov 30, 2017 at 11:39
  • I know it's not, that's why I made it a comment not an answer :-) Commented Nov 30, 2017 at 11:40

3 Answers 3

1

Not sure what you try to achieve here but a better option would be to use the DateTime Class

something like that should work

$day = "1";
$month = "11";
$year = "2017";
$objDateStart = DateTimeImmutable::createFromFormat('j-m-Y', $day."-".$month."-".$year);
$objDateEnd = $objDateStart->modify('+1 week');

$objDateRange = new DatePeriod($objDateStart, new DateInterval('P1D'), $objDateEnd);
foreach($objDateRange as $objDate)
{
    echo  '<th align="center"> '. $objDate->format("m-d-Y l"). '</th>';
}
Sign up to request clarification or add additional context in comments.

Comments

0
As it is i have executed your code, i am getting proper output without any repeated values. 

echo "<table><thead>

        <th>Project</th>

        <th>Activity</th>

        <th>Bill Type</th>";


        $day = "1";
        $month = "11";
        $year = "2017";

        $start_date = $day."-".$month."-".$year;
        $start_time = strtotime($start_date);

        $end_time = strtotime("+1 week", $start_time);

        for($i=$start_time; $i<$end_time; $i+=86400)
        {
          print   '<th align="center"> '. date("m-d-Y  l", $i). '</th>';
        }
        echo "<th>Hours</th>
        </thead>";

4 Comments

but iam getting like this: 11-01-2017 Wednesday 11-02-2017 Thursday 11-03-2017 Friday 11-04-2017 Saturday 11-05-2017 Sunday 11-05-2017 Sunday 11-06-2017 Monday 11-07-2017 Tuesday
i got two sundays at a time
if it is possible please try to post full code... then we can find the mistake. As of now what i have posted with code that is working fine. have you tried my code?
please see my picture above
0

The date command is repeating Nov 5th because that was daylight saving time and it's rounding down the hours to 00:00:00.

Try using the datetime object instead.

Here's an answer that loops through a date range with it.

https://stackoverflow.com/a/29765247/3585500

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.