如何设置"一周的第一天"到星期四的PHP

时间:2015-09-15 03:13:27

标签: php function date

我想将一周的第一天设置为星期四(不是星期日或星期一),因为这是该公司的截止日期。

我已经有一个代码来确定一个日期的当前周数,但是它从周日或周一开始。

如何根据我的偏好修改这些?

function findweek($date) {
    $monthstart=date("N",strtotime(date("n/l/Y",strtotime($date))));
    $newdate=(date("j",strtotime($date))+$monthstart)/7;
    $ddate=floor($newdate);
    if($ddate != $date) {
        $ddate++;
    }
    return $ddate;
}

2 个答案:

答案 0 :(得分:2)

http://php.net/manual/en/datetime.formats.relative.php说自PHP版本5.6.23起,7.0.8" 周总是从星期一开始。以前,周日也会被认为是开始一周。"也就是说,根据今天是在今周的星期四还是在当周的星期四之前,返回的周数可能不正确?也许尝试这样的事情:

$date = new DateTime();
$week = intval($date->format('W'));
$day = intval($date->format('N'));
echo $day < 4 ? $week-1 : $week;

如果减去1并不是你可以使用加法/减法来回答的答案,那么在得到正确的公式之前,将结果与你知道的真实答案进行比较。希望这有帮助!

答案 1 :(得分:1)

这应该有用。

#recursively crawls the directory tree and converts pdf files to tif files
#compresses the images to a smaller file size.

$Now = get-date
$Days = "14"
$LastWrite = $Now.AddDays(-$Days)
$srcfolder = "E:\DocuTA\PDFs\"
$destfolder = "E:\DocuTA\TIFs\"
$convert = "& C:\Program Files (x86)\gs\gs9.16\bin\gswin32c.exe"
$filter = "*.pdf"
$dest_ext = "tif"
$arg1 = @(" -dNOPAUSE -dQUIET -r300x300 -sDEVICE=tiffg4 -dBATCH ")
$lfpath = "E:\DocuTA\Logs\"
$logfile = new-item -type file -name "PDF2TIFLog$(get-date -uformat '%Y%m%d%H%M%S').log" -path "$lfpath"
$elogfile = "E:\DocuTA\Logs\PDF2TIFErrorLog.log"
#-------------------------------------------------------------------
$count = 0
Write-Output "`nStarting ... ($Now)" | Out-File -Append $logfile
Try
{
foreach ($srcitem in $(Get-ChildItem $srcfolder -Include $filter -Recurse | Where-Object {$_.LastWriteTime -gt "$LastWrite"}))
{
$count++
$srcname = $srcitem.FullName
$partial = $srcitem.FullName.Substring($srcfolder.Length)
$destname = $destfolder+$partial
$destname = [System.IO.Path]::ChangeExtension($destname,$dest_ext)
$destpath = [System.IO.Path]::GetDirectoryName($destname)
$cmdline = "'"+$convert+"'"+"'"+$srcname+"'"+$arg1+ "'"+$destname+"'"
Write-Output "[$count] $cmdline" | Out-File -Append $logfile
$ret = invoke-expression -command $cmdline
Write-Output "[$count] OUTPUT: $ret" | Out-File -Append $logfile
Write-Output "[$count] processed file." | Out-File -Append $logfile
}
}
Catch
{
$Now | out-file -Append $elogfile
write-output "ERROR converting File $destname" | out-file -Append $elogfile
Write-Output $Error[0] | out-file -Append $elogfile
}
Finally 
{
Write-Output "Finished processing files. Total: $count`n" | Out-File -Append $logfile
}