迄今增加X天,不包括周末 [英] Add X Days To Date, Excluding Weekends

查看:84
本文介绍了迄今增加X天,不包括周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个在线订阅应用程序.我在用户选择要订阅的天数然后选择开始日期方面遇到了一些挑战.然后,PHP应用程序应该能够计算结束日期(不包括周末).

I'm currently developing an online subscription application. I'm having some challenges on the part where the user will select the Number of Days to subscribe and then the Start Date. The PHP application should then be able to calculate the End Date excluding weekends.

<form method="post">
    <input name="startdate" type="text" />
    <input name="numberofdays" type="text" />
</form>

有人可以帮我吗?

推荐答案

取决于startdate的发送方式,但是假设您使用的是Ymd,则可以使用

depends how the startdate is sent, but assuming you are using Y-m-d you can use DateTime

例如:

<?php 

    $_POST['startdate'] = '2012-08-14';
    $_POST['numberofdays'] = 10;

    $d = new DateTime( $_POST['startdate'] );
    $t = $d->getTimestamp();

    // loop for X days
    for($i=0; $i<$_POST['numberofdays']; $i++){

        // add 1 day to timestamp
        $addDay = 86400;

        // get what day it is next day
        $nextDay = date('w', ($t+$addDay));

        // if it's Saturday or Sunday get $i-1
        if($nextDay == 0 || $nextDay == 6) {
            $i--;
        }

        // modify timestamp, add 1 day
        $t = $t+$addDay;
    }

    $d->setTimestamp($t);

    echo $d->format( 'Y-m-d' ). "\n";

?>

这篇关于迄今增加X天,不包括周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆