如何使用php查找开始日期和结束日期介于两天之间的周数


how to find number of weeks with start and end date between two days using php

  1. 如何在PHP中找到开始和结束日期介于两个日期之间的周数?例如:

2015年4月1日2015-04-30

function count_weeks_and_days($from, $to) {
    $day   = 24 * 3600;
    $from  = strtotime($from);
    $to    = strtotime($to) + $day;
    $diff  = abs($to - $from);
    $weeks = floor($diff / $day / 7);
    $days  = $diff / $day - $weeks * 7;
    $out   = array();
    if ($weeks) $out[] = "$weeks Week" . ($weeks > 1 ? 's' : '');
    if ($days)  $out[] = "$days Day" . ($days > 1 ? 's' : '');
    return implode(', ', $out);
}

您可以将日期转换为unix时间,然后计算周数。

<?php
$date1=strtotime($date1);
$date2=strtotime($date2);
$weeks=( $date2>$date1? $date2-$date1 : $date1-$date2)/(60*60*24*7);
echo "Number of weeks : ".floor($weeks);
?>