ikunyemingor/first-or-last-period-date

在PHP中获取周、月、季度或年份的第一天或最后一天

dev-master / 0.1.x-dev 2019-01-25 13:09 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:42:02 UTC


README

在PHP中获取周、月、季度或年份的第一天或最后一天是针对 @davgothic First/Last Day Period 2013年的作品 进行轻微修改。

如果您曾经需要找到指定期间的第一天或最后一天,并且您的PHP版本大于或等于5.2,那么今天对您来说是个幸运的日子!
这两个函数都返回DateTime对象,因此您可以使用PHP date()函数的任何可用格式输出日期。

演示和示例

请查看CodePad进行演示。

安装

需求

PHP >= 5.2

使用Composer - 基本用法

$ composer require ikunyemingor/first-or-last-period-date

{
    "require": {
        "ikunyemingor/first-or-last-period-date": ">=0.1.1"
    }
}
<?php

require 'vendor/autoload.php';

use LostCodes\PeriodDate;

$pd = new PeriodDate();

// Get current week first day.
$date = $pd->firstDayOf('week');
echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n";

// Get current week last day.
$date = $pd->lastDayOf('week');
echo 'The last day of the current week is: ' . $date->format('l, jS F Y') . "\n\n";

// Get first day of each week between two dates.
print_r($pd->getWeekFirstDayBetweenDates("2018-01-08", date('Y-m-d')));

?>

不使用Composer - 基本用法

<?php

use LostCodes\PeriodDate;

require_once "includes/PeriodDate.php";

$pd = new PeriodDate();

// Get current week first day.
$date = $pd->firstDayOf('week');
echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n";

// Get current week last day.
$date = $pd->lastDayOf('week');
echo 'The last day of the current week is: ' . $date->format('l, jS F Y') . "\n\n";

// Get first day of each week between two dates.
print_r($pd->getWeekFirstDayBetweenDates("2018-01-08", date('Y-m-d')));

// Get all day dates between two dates.
print_r($pd->getDayDatesBetweenTwoDates("2018-01-08", date('Y-m-d')));

?>

更多示例

<?php

$pd = new PeriodDate();
// Get today.
echo 'Today is: ' . date("l, jS F Y", strtotime("today")) . "\n\n";
// Get current week first and last day.
$date = $pd->firstDayOf('week');
echo 'The first day of the current week is: ' . $date->format('l, jS F Y') . "\n";
$date = $pd->lastDayOf('week');
echo 'The last day of the current week is: ' . $date->format('l, jS F Y') . "\n\n";

// Get current month first and last day.
$date = $pd->firstDayOf('month');
echo 'The first day of the current month is: ' . $date->format('l, jS F Y') . "\n";
$date = $pd->lastDayOf('month');
echo 'The last day of the current month is: ' . $date->format('l, jS F Y') . "\n\n";

// Get current year first and last day.
$date = $pd->firstDayOf('year');
echo 'The first day of the current year is: ' . $date->format('l, jS F Y') . "\n";
$date = $pd->lastDayOf('year');
echo 'The last day of the current year is: ' . $date->format('l, jS F Y') . "\n\n";

// Get yesterday.
echo 'Yesterday is: ' . date("l, jS F Y", strtotime("yesterday")) . "\n\n";
// Get previous week first and last day.
$specifiedDate = new DateTime(date('Y'));
$date          = $pd->firstDayOf('week', $specifiedDate, "last");
echo 'The first day of the previous week is: ' . $date->format('l, jS F Y') . "\n";
$date = $pd->lastDayOf('week', $specifiedDate, "last");
echo 'The last day of the previous week is: ' . $date->format('l, jS F Y') . "\n\n";

// Get previous month first and last day.
$specifiedDate = new DateTime(date('Y'));
$date          = $pd->firstDayOf('month', $specifiedDate, "last");
echo 'The first day of the previous month is: ' . $date->format('l, jS F Y') . "\n";
$date = $pd->lastDayOf('month', $specifiedDate, "last");
echo 'The last day of the previous month is: ' . $date->format('l, jS F Y') . "\n\n";

// Get previous year first and last day.
$specifiedDate = new DateTime(date('Y') - 1);
$date          = $pd->firstDayOf('year', $specifiedDate, "last");
echo 'The first day of the previous year is: ' . $date->format('l, jS F Y') . "\n";
$date = $pd->lastDayOf('year', $specifiedDate, "last");
echo 'The last day of the previous year is: ' . $date->format('l, jS F Y') . "\n\n";

// Get first day of each week between two dates with first day of week as Monday and custom returned date format.
print_r($pd->getWeekFirstDayBetweenDates("2018-01-08", date('Y-m-d'), "1", date('l, jS F Y')));

// Get all day dates between two dates with an interval and custom returned date format.
print_r($pd->getDayDatesBetweenTwoDates("2018-11-01", date('Y-m-d'), 1, date('l, jS F Y')));

?>

变更

在此查看变更日志这里

贡献者

Ikunyemi Ngor

许可证

Apache License 2.0下发布