dqf98772 2013-12-24 17:35
浏览 47
已采纳

使用php中的特定日期自动获取结束日期[关闭]

When Start date is given it should get the end date by fetching only sunday or mon, tue, wed or ,mon to fri or tue, thurs, sat using php. pls anyone help me.....


Here is my html code:




 <label>Start Date:</label><input type="text" id="txtStartDate" name="sd"></input>
 <td>days</td>
<td>
<select name="days">
    <option value="" >only sunday</option> 
    <option value="batch">sat and sun</option>
    <option value="">M-F</option>
    <option value="">M-W-F</option>
    <option value="">TU-THUR-SAT</option>
 </td>
 <td>
    <input type="submit" value="Submit" name="sub"  />
 </td>

<?php
//$startdate=$_POST['sd'];
$startdate='2013-12-25';
$date=date_create($startdate);
date_add($date,date_interval_create_from_date_string("10 days"));
echo date_format($date,"Y-m-d");
?>

it only fetches using number of days but i want to get using specific days.

  • 写回答

2条回答 默认 最新

  • dpp66953 2013-12-24 18:58
    关注

    Store that days that you want to use in the option values ie

    <select name="days">
        <option value="sunday" >only sunday</option> 
        <option value="saturday-sunday">sat and sun</option>
        <option value="monday-tuesday-wednesday-thursday-friday">Monday to Friday</option>
        <option value="monday-wednesday">Only Monday or Wednesday</option>
    </select>
    

    if (isset($_POST['sd']) && isset($_POST['days'])) {
    
        $startDate = new DateTime($_POST['sd']);
        $days = explode('-', $_POST['days']);
        $count = count($days);
        $dates = array();
    
        $currentDate = $startDate->format('Y-m-d');
        $j = 0;
    
        for ($i=0;$i<10;$i++) {
    
            $day = $days[$j];
            $a = new DateTime($currentDate);
            $a->modify("next $day");
            $dates[] = $currentDate = $a->format('Y-m-d');
    
            $j = (($j+1) == $count) ? 0 : $j++; 
    
        } 
    
        foreach ($dates as $date) {
            echo $date.'<br />';
        }
    }
    

    Hope this helps! :)

    EDIT

    or if you only want the last one of the array:

    instead of the foreach use

    echo end($dates);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统