繁体   English   中英

如何计算php中每个月的天数

[英]How to calculate number of days in each month in php

需要在PHP中计算从当前日期到每月27日的天数在下面的代码中,它正确计算当前月份,但如果当前日期是28日,则应计算下个月。

$year = date("y");
$month = date("m");
$day = '27';

$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');

尝试php cal_days_in_month函数

cal_days_in_month — Return the number of days in a month for a given year and calendar

例如:

$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There were {$number} days in August 2003";

参考

我快速编写了这个脚本,因为我没有时间测试它。

编辑:

$day = 27;
$today = date('d');

if($today < $day){
    $math = $day - $today;
    echo "There are " . $math . " days left until the 27th.";
} else {
    $diff = date('t') - $today;

    $math = $diff + $day;
    echo "There are " . $math . " days left until the 27th of the next month.";
}

试试下面的代码,

<?php
    $year = date("y");
    $month = date("m");
    $day = '27';

    $current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
    $end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
    if($current_date->getTimestamp()<=$end_date->getTimestamp()){
        $interval = $current_date->diff($end_date);
        echo $interval->format('%a day(s)');
    }
    else{
        $interval = $end_date->diff($current_date);
        echo $interval->format('-%a day(s)');
    }
?>
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;

echo floor($datediff / (60 * 60 * 24));

来源: 查找两个日期之间的天数

这样....

<?php
  $d=cal_days_in_month(CAL_GREGORIAN,10,2005);
  echo "There was $d days in October 2005";
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM