Skip to content
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
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
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
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
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

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
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
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