Combined\Datetime date/number to weekday conversion
Convert functions for date or weekday number to weekday name or date to weekday number
This commit is contained in:
@@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase;
|
|||||||
*/
|
*/
|
||||||
final class CoreLibsCombinedDateTimeTest extends TestCase
|
final class CoreLibsCombinedDateTimeTest extends TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* timestamps
|
* timestamps
|
||||||
*
|
*
|
||||||
@@ -618,13 +617,169 @@ final class CoreLibsCombinedDateTimeTest extends TestCase
|
|||||||
* @param array $expected
|
* @param array $expected
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testCalcDaysInterval(string $input_a, string $input_b, bool $flag, $expected): void
|
public function testCalcDaysInterval(
|
||||||
{
|
string $input_a,
|
||||||
|
string $input_b,
|
||||||
|
bool $flag,
|
||||||
|
$expected
|
||||||
|
): void {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$expected,
|
||||||
\CoreLibs\Combined\DateTime::calcDaysInterval($input_a, $input_b, $flag)
|
\CoreLibs\Combined\DateTime::calcDaysInterval($input_a, $input_b, $flag)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function weekdayNumberProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'0 invalid' => [0, null, 'Inv',],
|
||||||
|
'0 invalid long' => [0, true, 'Invalid',],
|
||||||
|
'1 short' => [1, null, 'Mon',],
|
||||||
|
'1 long' => [1, true, 'Monday',],
|
||||||
|
'2 short' => [2, null, 'Tue',],
|
||||||
|
'2 long' => [2, true, 'Tuesday',],
|
||||||
|
'3 short' => [3, null, 'Wed',],
|
||||||
|
'3 long' => [3, true, 'Wednesday',],
|
||||||
|
'4 short' => [4, null, 'Thu',],
|
||||||
|
'4 long' => [4, true, 'Thursday',],
|
||||||
|
'5 short' => [5, null, 'Fri',],
|
||||||
|
'5 long' => [5, true, 'Friday',],
|
||||||
|
'6 short' => [6, null, 'Sat',],
|
||||||
|
'6 long' => [6, true, 'Saturday',],
|
||||||
|
'7 short' => [7, null, 'Sun',],
|
||||||
|
'7 long' => [7, true, 'Sunday',],
|
||||||
|
'8 invalid' => [8, null, 'Inv',],
|
||||||
|
'8 invalid long' => [8, true, 'Invalid',],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* int weekday number to string weekday
|
||||||
|
*
|
||||||
|
* @covers ::setWeekdayNameFromIsoDow
|
||||||
|
* @dataProvider weekdayNumberProvider
|
||||||
|
* @testdox weekdayListProvider $input (short $flag) will be $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param int $input
|
||||||
|
* @param bool|null $flag
|
||||||
|
* @param string $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetWeekdayNameFromIsoDow(
|
||||||
|
int $input,
|
||||||
|
?bool $flag,
|
||||||
|
string $expected
|
||||||
|
): void {
|
||||||
|
if ($flag === null) {
|
||||||
|
$output = \CoreLibs\Combined\DateTime::setWeekdayNameFromIsoDow($input);
|
||||||
|
} else {
|
||||||
|
$output = \CoreLibs\Combined\DateTime::setWeekdayNameFromIsoDow($input, $flag);
|
||||||
|
}
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
$output
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function weekdayDateProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'invalid date' => ['2022-02-31', -1],
|
||||||
|
'1: monday' => ['2022-07-25', 1],
|
||||||
|
'2: tuesday' => ['2022-07-26', 2],
|
||||||
|
'3: wednesday' => ['2022-07-27', 3],
|
||||||
|
'4: thursday' => ['2022-07-28', 4],
|
||||||
|
'5: friday' => ['2022-07-29', 5],
|
||||||
|
'6: saturday' => ['2022-07-30', 6],
|
||||||
|
'7: sunday' => ['2022-07-31', 7],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* date to weekday number
|
||||||
|
*
|
||||||
|
* @covers ::setWeekdayNumberFromDate
|
||||||
|
* @dataProvider weekdayDateProvider
|
||||||
|
* @testdox setWeekdayNumberFromDate $input will be $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param int $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetWeekdayNumberFromDate(
|
||||||
|
string $input,
|
||||||
|
int $expected
|
||||||
|
): void {
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Combined\DateTime::setWeekdayNumberFromDate($input)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function weekdayDateNameProvider(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'invalid date short' => ['2022-02-31', null, 'Inv'],
|
||||||
|
'invalid date long' => ['2022-02-31', true, 'Invalid'],
|
||||||
|
'Mon short' => ['2022-07-25', null, 'Mon'],
|
||||||
|
'Monday long' => ['2022-07-25', true, 'Monday'],
|
||||||
|
'Tue short' => ['2022-07-26', null, 'Tue'],
|
||||||
|
'Tuesday long' => ['2022-07-26', true, 'Tuesday'],
|
||||||
|
'Wed short' => ['2022-07-27', null, 'Wed'],
|
||||||
|
'Wednesday long' => ['2022-07-27', true, 'Wednesday'],
|
||||||
|
'Thu short' => ['2022-07-28', null, 'Thu'],
|
||||||
|
'Thursday long' => ['2022-07-28', true, 'Thursday'],
|
||||||
|
'Fri short' => ['2022-07-29', null, 'Fri'],
|
||||||
|
'Friday long' => ['2022-07-29', true, 'Friday'],
|
||||||
|
'Sat short' => ['2022-07-30', null, 'Sat'],
|
||||||
|
'Saturday long' => ['2022-07-30', true, 'Saturday'],
|
||||||
|
'Sun short' => ['2022-07-31', null, 'Sun'],
|
||||||
|
'Sunday long' => ['2022-07-31', true, 'Sunday'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* date to weekday name
|
||||||
|
*
|
||||||
|
* @covers ::setWeekdayNameFromDate
|
||||||
|
* @dataProvider weekdayDateNameProvider
|
||||||
|
* @testdox setWeekdayNameFromDate $input (short $flag) will be $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param bool|null $flag
|
||||||
|
* @param string $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSetWeekdayNameFromDate(
|
||||||
|
string $input,
|
||||||
|
?bool $flag,
|
||||||
|
string $expected
|
||||||
|
): void {
|
||||||
|
if ($flag === null) {
|
||||||
|
$output = \CoreLibs\Combined\DateTime::setWeekdayNameFromDate($input);
|
||||||
|
} else {
|
||||||
|
$output = \CoreLibs\Combined\DateTime::setWeekdayNameFromDate($input, $flag);
|
||||||
|
}
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
$output
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
@@ -13,6 +13,11 @@ use PHPUnit\Framework\TestCase;
|
|||||||
*/
|
*/
|
||||||
final class CoreLibsConvertStringsTest extends TestCase
|
final class CoreLibsConvertStringsTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function splitFormatStringProvider(): array
|
public function splitFormatStringProvider(): array
|
||||||
{
|
{
|
||||||
// 0: input
|
// 0: input
|
||||||
|
|||||||
@@ -139,6 +139,22 @@ foreach ($compare_dates as $compare_date) {
|
|||||||
. DgS::printAr(DateTime::calcDaysInterval($compare_date[0], $compare_date[1], true)) . "<br>";
|
. DgS::printAr(DateTime::calcDaysInterval($compare_date[0], $compare_date[1], true)) . "<br>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test date conversion
|
||||||
|
$dow = 2;
|
||||||
|
print "DOW[$dow]: " . DateTime::setWeekdayNameFromIsoDow($dow) . "<br>";
|
||||||
|
print "DOW[$dow],long: " . DateTime::setWeekdayNameFromIsoDow($dow, true) . "<br>";
|
||||||
|
$date = '2022-7-22';
|
||||||
|
print "DATE-dow[$date]: " . DateTime::setWeekdayNameFromDate($date) . "<br>";
|
||||||
|
print "DATE-dow[$date],long: " . DateTime::setWeekdayNameFromDate($date, true) . "<br>";
|
||||||
|
print "DOW-date[$date]: " . DateTime::setWeekdayNumberFromDate($date) . "<br>";
|
||||||
|
$dow = 11;
|
||||||
|
print "DOW[$dow];invalid: " . DateTime::setWeekdayNameFromIsoDow($dow) . "<br>";
|
||||||
|
print "DOW[$dow],long;invalid: " . DateTime::setWeekdayNameFromIsoDow($dow, true) . "<br>";
|
||||||
|
$date = '2022-70-242';
|
||||||
|
print "DATE-dow[$date];invalid: " . DateTime::setWeekdayNameFromDate($date) . "<br>";
|
||||||
|
print "DATE-dow[$date],long;invalid: " . DateTime::setWeekdayNameFromDate($date, true) . "<br>";
|
||||||
|
print "DOW-date[$date];invalid: " . DateTime::setWeekdayNumberFromDate($date) . "<br>";
|
||||||
|
|
||||||
// error message
|
// error message
|
||||||
print $log->printErrorMsg();
|
print $log->printErrorMsg();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* image thumbnail, rotate, etc
|
* date convert and check functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
@@ -193,6 +193,54 @@ class DateTime
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns long or short day of week name based on ISO day of week number
|
||||||
|
* 1: Monday
|
||||||
|
* ...
|
||||||
|
* 7: Sunday
|
||||||
|
*
|
||||||
|
* @param int $isodow 1: Monday, 7: Sunday
|
||||||
|
* @param bool $long Default false 'Mon', if true 'Monday'
|
||||||
|
* @return string Day of week string either short 'Mon' or long 'Monday'
|
||||||
|
*/
|
||||||
|
public static function setWeekdayNameFromIsoDow(int $isodow, bool $long = false): string
|
||||||
|
{
|
||||||
|
// if not valid, set to invalid
|
||||||
|
if ($isodow < 1 || $isodow > 7) {
|
||||||
|
return $long ? 'Invalid' : 'Inv';
|
||||||
|
}
|
||||||
|
return date($long ? 'l' : 'D', strtotime("Sunday +{$isodow} days"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the day of week Name from date
|
||||||
|
*
|
||||||
|
* @param string $date Any valid date
|
||||||
|
* @param bool $long Default false 'Mon', if true 'Monday'
|
||||||
|
* @return string Day of week string either short 'Mon' or long 'Monday'
|
||||||
|
*/
|
||||||
|
public static function setWeekdayNameFromDate(string $date, bool $long = false): string
|
||||||
|
{
|
||||||
|
if (!self::checkDate($date)) {
|
||||||
|
return $long ? 'Invalid' : 'Inv';
|
||||||
|
}
|
||||||
|
return date($long ? 'l' : 'D', strtotime($date));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the day of week Name from date
|
||||||
|
*
|
||||||
|
* @param string $date Any valid date
|
||||||
|
* @return int ISO Weekday number 1: Monday, 7: Sunday, -1 for invalid date
|
||||||
|
*/
|
||||||
|
public static function setWeekdayNumberFromDate(string $date): int
|
||||||
|
{
|
||||||
|
if (!self::checkDate($date)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return (int)date('N', strtotime($date));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* splits & checks date, wrap around for check_date function
|
* splits & checks date, wrap around for check_date function
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user