如何利用php语法计算天数

2025年01月24日 建站教程

如何利用php语法给指定年份计算天数,下面web建站小编给大家简单介绍一下具体实现方法!

方法一:指定年的天数

function countDaysInYear($year) {
  if ($year % 4 == 0) {
    if ($year % 100 == 0 && $year % 400 != 0) {
      return 365;
    } else {
      return 366;
    }
  } else {
    return 365;
  }
}

echo countDaysInYear(2023);

方法二:指定日期的天数

function getDayOfYear($date) {
  $now = strtotime($date);
  $start = strtotime(date('Y', $now) .'-01-01');
  return floor(($now - $start) / (60 * 60 * 24)) + 1;
}

echo getDayOfYear('2023-01-22');

本文链接:http://so.lmcjl.com/news/21944/

展开阅读全文
相关内容