Php 检测星期六和星期日,并将x个天数添加到星期一
如果有人选择星期五,我希望它自动添加星期一之前的天数。 想象一下,Php 检测星期六和星期日,并将x个天数添加到星期一,php,date,date-math,Php,Date,Date Math,如果有人选择星期五,我希望它自动添加星期一之前的天数。 想象一下,$leavefrom是2014年1月3日,即周四,$leaveto是2014年2月3日,即周五$totaldays基于日期计算。因此是2天 <?php $x = 0; $date1 = str_replace('-', '/', $leavefrom); $date2 = str_replace('-', '/', $leaveto); while ($x < $totaldays) { $tomorro
$leavefrom
是2014年1月3日,即周四,$leaveto
是2014年2月3日,即周五<代码>$totaldays基于日期计算。因此是2天
<?php
$x = 0;
$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);
while ($x < $totaldays) {
$tomorrow = date('l', strtotime($date1 ."+1 days"));
//$tomorrow = date("m-d-Y", strtotime( $date1 ."+1 days" ));
$getday = date('D', strtotime($tomorrow));
$x++;
if ($getday == "Sunday" || $getday = "Saturday") {
$tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
}
$tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
}
echo $tomorrow;
?>
如果您只是想跳过周末,只需查看
$date2
是否在周末,如果是,请提前跳到下周一
$date2 = DateTime::CreateFromFormat('n-j-Y', $leaveto);
if (in_array($date2->format('l'), array('Sunday', 'Saturday'))) {
$date2->modify('next Monday');
}
echo $date2->format("m/d/Y");
尝试将if($getday==“Sunday”| |$getday=“Saturday”)
改为while
,然后去掉最后一个$tomory=…
。大概是这样的:
<?php
$x = 0;
$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);
while ($x < $totaldays) {
$tomorrow = date('l', strtotime($date1 ."+1 days"));
$x++;
$getday = date('D', strtotime($tomorrow));
while ($getday == "Sunday" || $getday = "Saturday") {
$tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
$getday = date('D', strtotime($tomorrow));
}
}
echo $tomorrow;
?>
我因为愚蠢在墙上撞了三个小时才找到解决办法,下面是我的代码:
while ($daysloop <= $totaldays) {
$tomorrow1 = date("m/d/Y", strtotime( $tomorrow1 ."+1 days" ));
$dayofweek = date('w', strtotime($tomorrow1));
if ($dayofweek == 0 || $dayofweek == 6) {
$weekends = $weekends + 1;
}
$daysloop++;
}
if ($totaldays == 0) {
$totaldays = $totaldays - $weekends + 1;
}
else {
$totaldays = $totaldays - $weekends;
}
while($daysloop到底是什么问题?你能试着更清楚地说明你想要什么吗?(我看到星期六、星期一,加起来直到星期五……我有点困惑;))好吧,你看,我想创建一个简单的休假系统,所以如果有人在上述两个日期申请休假,两个日期都是星期四和星期五,因此,申请休假的用户将在周一开始工作。我需要它的日期为周一跳过周六和周日。因此,他们将于2013年5月3日开始工作。明白我的意思吗?呵呵,对不起,英语很差。你确定这个代码是正确的吗?我得到无限循环。不,一点也不。你没有给我们足够的钱来测试,所以我不知道它是否有效。为什么不在循环中放置一个echo
,打印出$tomory
和$getday
的值,看看它为什么不退出。