Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add isWeekend() and isWeekday() methods to DateTime classes #19734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
xentixar wants to merge 3 commits into php:master
base: master
Choose a base branch
Loading
from xentixar:add-weekend-and-weekday-methods-to-datetime-class
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ext/date/lib/dow.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d)
return timelib_day_of_week_ex(y, m, d, 1);
}

/* Checks if the given date is a weekend (Saturday or Sunday) */
int timelib_is_weekend(timelib_time *time)
{
timelib_sll dow;

/* Ensure time parts are up to date */
if (!time->have_date) {
return 0;
}

dow = timelib_day_of_week(time->y, time->m, time->d);
/* 0 = Sunday, 6 = Saturday */
return (dow == 0 || dow == 6);
}

/* Checks if the given date is a weekday (Monday to Friday) */
int timelib_is_weekday(timelib_time *time)
{
timelib_sll dow;

/* Ensure time parts are up to date */
if (!time->have_date) {
return 0;
}

dow = timelib_day_of_week(time->y, time->m, time->d);
/* 1 = Monday through 5 = Friday */
return (dow >= 1 && dow <= 5);
}

/* jan feb mar apr may jun jul aug sep oct nov dec */
static int d_table_common[13] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
static int d_table_leap[13] = { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
Expand Down
6 changes: 6 additions & 0 deletions ext/date/lib/timelib.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ timelib_sll timelib_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d);
/* Calculates the day of the ISO week from y, m, and d. 1=Monday, 7=Sunday */
timelib_sll timelib_iso_day_of_week(timelib_sll y, timelib_sll m, timelib_sll d);

/* Checks if the given date is a weekend (Saturday or Sunday) */
int timelib_is_weekend(timelib_time *time);

/* Checks if the given date is a weekday (Monday to Friday) */
int timelib_is_weekday(timelib_time *time);

/* Calculates the day of the year according to y-m-d. 0=Jan 1st..364/365=Dec
* 31st */
timelib_sll timelib_day_of_year(timelib_sll y, timelib_sll m, timelib_sll d);
Expand Down
56 changes: 56 additions & 0 deletions ext/date/php_date.c
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2809,6 +2809,62 @@ PHP_METHOD(DateTimeImmutable, createFromTimestamp)
}
/* }}} */

/* {{{ Checks if the given date is a weekend (Saturday or Sunday) */
PHP_METHOD(DateTime, isWeekend)
{
php_date_obj *dateobj;

ZEND_PARSE_PARAMETERS_NONE();

dateobj = Z_PHPDATE_P(ZEND_THIS);
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(ZEND_THIS));

RETURN_BOOL(timelib_is_weekend(dateobj->time));
}
/* }}} */

/* {{{ Checks if the given date is a weekday (Monday to Friday) */
PHP_METHOD(DateTime, isWeekday)
{
php_date_obj *dateobj;

ZEND_PARSE_PARAMETERS_NONE();

dateobj = Z_PHPDATE_P(ZEND_THIS);
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(ZEND_THIS));

RETURN_BOOL(timelib_is_weekday(dateobj->time));
}
/* }}} */

/* {{{ Checks if the given date is a weekend (Saturday or Sunday) */
PHP_METHOD(DateTimeImmutable, isWeekend)
{
php_date_obj *dateobj;

ZEND_PARSE_PARAMETERS_NONE();

dateobj = Z_PHPDATE_P(ZEND_THIS);
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(ZEND_THIS));

RETURN_BOOL(timelib_is_weekend(dateobj->time));
}
/* }}} */

/* {{{ Checks if the given date is a weekday (Monday to Friday) */
PHP_METHOD(DateTimeImmutable, isWeekday)
{
php_date_obj *dateobj;

ZEND_PARSE_PARAMETERS_NONE();

dateobj = Z_PHPDATE_P(ZEND_THIS);
DATE_CHECK_INITIALIZED(dateobj->time, Z_OBJCE_P(ZEND_THIS));

RETURN_BOOL(timelib_is_weekday(dateobj->time));
}
/* }}} */

static bool php_date_initialize_from_hash(php_date_obj **dateobj, const HashTable *myht)
{
zval *z_date;
Expand Down
18 changes: 18 additions & 0 deletions ext/date/php_date.stub.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ public function __wakeup(): void;
public function __serialize(): array;

public function __unserialize(array $data): void;

/** @tentative-return-type */
public function isWeekend(): bool;

/** @tentative-return-type */
public function isWeekday(): bool;
}

class DateTime implements DateTimeInterface
Expand Down Expand Up @@ -445,6 +451,12 @@ public function getTimestamp(): int {}
* @alias date_diff
*/
public function diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval {}

/** @tentative-return-type */
public function isWeekend(): bool {}

/** @tentative-return-type */
public function isWeekday(): bool {}
}

class DateTimeImmutable implements DateTimeInterface
Expand Down Expand Up @@ -552,6 +564,12 @@ public static function createFromMutable(DateTime $object): static {}

/** @return static */
public static function createFromInterface(DateTimeInterface $object): DateTimeImmutable {} // TODO return type should be static

/** @tentative-return-type */
public function isWeekend(): bool {}

/** @tentative-return-type */
public function isWeekday(): bool {}
}

class DateTimeZone
Expand Down
25 changes: 24 additions & 1 deletion ext/date/php_date_arginfo.h
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions ext/date/tests/DateTime_isWeekday_basic.phpt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
--TEST--
Test DateTime::isWeekday() function : basic functionality
--FILE--
<?php
// Set the default time zone
date_default_timezone_set("UTC");

echo "*** Testing DateTime::isWeekday() : basic functionality ***\n";

// Saturday (weekend)
$date = new DateTime("2025年09月06日");
echo "Saturday 2025年09月06日 is weekday: ";
var_dump($date->isWeekday());

// Sunday (weekend)
$date = new DateTime("2025年09月07日");
echo "Sunday 2025年09月07日 is weekday: ";
var_dump($date->isWeekday());

// Monday (weekday)
$date = new DateTime("2025年09月08日");
echo "Monday 2025年09月08日 is weekday: ";
var_dump($date->isWeekday());

// Tuesday (weekday)
$date = new DateTime("2025年09月09日");
echo "Tuesday 2025年09月09日 is weekday: ";
var_dump($date->isWeekday());

// Test with DateTimeImmutable
echo "\n*** Testing DateTimeImmutable::isWeekday() : basic functionality ***\n";

// Saturday (weekend)
$date = new DateTimeImmutable("2025年09月06日");
echo "Saturday 2025年09月06日 is weekday: ";
var_dump($date->isWeekday());

// Sunday (weekend)
$date = new DateTimeImmutable("2025年09月07日");
echo "Sunday 2025年09月07日 is weekday: ";
var_dump($date->isWeekday());

// Monday (weekday)
$date = new DateTimeImmutable("2025年09月08日");
echo "Monday 2025年09月08日 is weekday: ";
var_dump($date->isWeekday());

// Tuesday (weekday)
$date = new DateTimeImmutable("2025年09月09日");
echo "Tuesday 2025年09月09日 is weekday: ";
var_dump($date->isWeekday());
?>
--EXPECT--
*** Testing DateTime::isWeekday() : basic functionality ***
Saturday 2025年09月06日 is weekday: bool(false)
Sunday 2025年09月07日 is weekday: bool(false)
Monday 2025年09月08日 is weekday: bool(true)
Tuesday 2025年09月09日 is weekday: bool(true)

*** Testing DateTimeImmutable::isWeekday() : basic functionality ***
Saturday 2025年09月06日 is weekday: bool(false)
Sunday 2025年09月07日 is weekday: bool(false)
Monday 2025年09月08日 is weekday: bool(true)
Tuesday 2025年09月09日 is weekday: bool(true)
52 changes: 52 additions & 0 deletions ext/date/tests/DateTime_isWeekend_basic.phpt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
Test DateTime::isWeekend() function : basic functionality
--FILE--
<?php
// Set the default time zone
date_default_timezone_set("UTC");

echo "*** Testing DateTime::isWeekend() : basic functionality ***\n";

// Saturday (weekend)
$date = new DateTime("2025年09月06日");
echo "Saturday 2025年09月06日 is weekend: ";
var_dump($date->isWeekend());

// Sunday (weekend)
$date = new DateTime("2025年09月07日");
echo "Sunday 2025年09月07日 is weekend: ";
var_dump($date->isWeekend());

// Monday (weekday)
$date = new DateTime("2025年09月08日");
echo "Monday 2025年09月08日 is weekend: ";
var_dump($date->isWeekend());

// Test with DateTimeImmutable
echo "\n*** Testing DateTimeImmutable::isWeekend() : basic functionality ***\n";

// Saturday (weekend)
$date = new DateTimeImmutable("2025年09月06日");
echo "Saturday 2025年09月06日 is weekend: ";
var_dump($date->isWeekend());

// Sunday (weekend)
$date = new DateTimeImmutable("2025年09月07日");
echo "Sunday 2025年09月07日 is weekend: ";
var_dump($date->isWeekend());

// Monday (weekday)
$date = new DateTimeImmutable("2025年09月08日");
echo "Monday 2025年09月08日 is weekend: ";
var_dump($date->isWeekend());
?>
--EXPECT--
*** Testing DateTime::isWeekend() : basic functionality ***
Saturday 2025年09月06日 is weekend: bool(true)
Sunday 2025年09月07日 is weekend: bool(true)
Monday 2025年09月08日 is weekend: bool(false)

*** Testing DateTimeImmutable::isWeekend() : basic functionality ***
Saturday 2025年09月06日 is weekend: bool(true)
Sunday 2025年09月07日 is weekend: bool(true)
Monday 2025年09月08日 is weekend: bool(false)
Loading

AltStyle によって変換されたページ (->オリジナル) /