1

考虑到闰年,我需要提取任何一个月的总小时数,只考虑月份和年份。

到目前为止,这是我的代码...

$MonthName = "January";
$Year = "2013";

$TimestampofMonth = strtotime("$MonthName  $Year");
$TotalMinutesinMonth = $TimestampofMonth / 60     // to convert to minutes
$TotalHoursinMonth = $TotalMinutesinMonth / 60    // to convert to hours
4

4 回答 4

1

只需计算该月的天数,然后乘以 24,如下所示:

// Set the date in any format
$date = '01/01/2013';
// another possible format etc...
$date = 'January 1st, 2013';

// Get the number of days in the month
$days = date('t', strtotime($date));

// Write out the days
echo $days;
于 2013-04-28T17:02:33.323 回答
1

你可以这样做:

<?php
$MonthName = "January";
$Year = "2013";
$days = date("t", strtotime("$MonthName 1st, $Year"));
echo $days * 24;
于 2013-04-28T16:57:09.990 回答
0

你可以使用DateTime::createFromFormat,因为你没有一天

$date = DateTime::createFromFormat("F Y", "January 2013");
printf("%s hr(s)",$date->format("t") * 24);

好吧,如果您正在查看工作日,那是一种不同的方法

$date = "January 2013"; // You only know Month and year
$workHours = 10; // 10hurs a day

$start = DateTime::createFromFormat("F Y d", "$date 1"); // added first
printf("%s hr(s)", $start->format("t") * 24);

// if you are only looking at working days

$end = clone $start;
$end->modify(sprintf("+%d day", $start->format("t") - 1));

$interval = new DateInterval("P1D"); // Interval
var_dump($start, $end);

$hr = 0;
foreach(new DatePeriod($start, $interval, $end) as $day) {  
    // Exclude sarturday & Sunday
    if ($day->format('N') < 6) {
        $hr += $workHours; // add working hours
    }
}
printf("%s hr(s)", $hr);
于 2013-04-28T17:03:36.657 回答
0
<?php

function get_days_in_month($month, $year)
{
  return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

$month = 4;
$year = 2013;

$total_hours = 24 * get_days_in_month($month, $year);


?>

您可以使用上述函数来检索考虑闰年的一个月中的总天数,然后将该值乘以 24 加,您也可以使用 cal_days_in_month 函数,但它仅支持 PHP 4.0.7 及更高版本的 PHP 构建。


如果您使用的是上面的“get_day_in_month”,那么您需要将字符串解析为整数,可以这样完成

一个月 <?php $date = date_parse('July'); $month_int = $date['month']; ?>

全年 <?php $year_string = "2013" $year_int = (int) $year_string ?>

于 2013-04-28T17:55:15.983 回答