English to Time - php

Does anyone know of a good class / library to convert English representations of time into timestamps?
The goal is to convert natural language phrases such as "ten years from now" and "three weeks" and "in 10 minutes" and working out a best match unix timestamp for them.
I have hacked up some pretty poor and untested code to get going on it, but I am sure there are great parsers out there for calendars and such.
private function timeparse($timestring)
{
$candidate = #strtotime($timestring);
if ($candidate > time()) return $candidate; // Let php have a bash at it
//$thisyear = date("Y");
if (strpos($timestring, "min") !== false) // Context is minutes
{
$nummins = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$nummins minutes");
return $candidate;
}
if (strpos($timestring, "hou") !== false) // Context is hours
{
$numhours = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$numhours hours");
return $candidate;
}
if (strpos($timestring, "day") !== false) // Context is days
{
$numdays = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$numdays days");
return $candidate;
}
if (strpos($timestring, "year") !== false) // Context is years (2 years)
{
$numyears = preg_replace("/\D/", "", $timestring);
$candidate = #strtotime("now +$numyears years");
return $candidate;
}
if (strlen($timestring) < 5) // 10th || 2nd (or probably a number)
{
$day = preg_replace("/\D/", "", $timestring);
if ($day > 0)
{
$month = date("m");
$year = date("y");
return strtotime("$month/$day/$year");
}
else
{
return false;
}
}
return false; // No can do.
}

Use the DateTime class.
e.g.:
$string='four days ago';
$d=date_create($string);
$d->getTimestamp();
ETA:
which you could extend:
class myDateTime extends DateTime {
static $defined_expressions=array(...);
function __construct($expression=NULL) {
if ($exp=$this->translate($expression)) {
parent::__construct($exp);
}
}
function translate($exp) {
//check to see if strtotime errors or not
//if it errors, check if $exp matches a pattern in self::$defined_expressions
return $exp, modified $exp or false
}
}

Sometime ago I had come across http://www.timeapi.org which converts natural language queries into time. It is an API though.
The ruby source code is on github. If need be, I guess you could try to port it to PHP.

Just got a notification from PHPClasses, with one of the runner-ups of the monthly innovation award: Text to Timestamp
You could try that...

Cocoa's and GNUStep's NSDateFormatter are able to handle such time representations. The GNUStep version is open-source.

Related

PHP DateTime::createFromFormat check issue [duplicate]

I'm receiving a date string from an API, and it is formatted as yyyy-mm-dd.
I am currently using a regex to validate the string format, which works ok, but I can see some cases where it could be a correct format according to the string but actually an invalid date. i.e. 2013-13-01, for example.
Is there a better way in PHP to take a string such as 2013-13-01 and tell if it is a valid date or not for the format yyyy-mm-dd?
You can use DateTime::createFromFormat() for this purpose:
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}
[Function taken from this answer. Also on php.net. Originally written by Glavić.]
Test cases:
var_dump(validateDate('2013-13-01')); // false
var_dump(validateDate('20132-13-01')); // false
var_dump(validateDate('2013-11-32')); // false
var_dump(validateDate('2012-2-25')); // false
var_dump(validateDate('2013-12-01')); // true
var_dump(validateDate('1970-12-01')); // true
var_dump(validateDate('2012-02-29')); // true
var_dump(validateDate('2012', 'Y')); // true
var_dump(validateDate('12012', 'Y')); // false
Demo!
Determine if any string is a date
function checkIsAValidDate($myDateString){
return (bool)strtotime($myDateString);
}
Use in simple way with php prebuilt function:
function checkmydate($date) {
$tempDate = explode('-', $date);
// checkdate(month, day, year)
return checkdate($tempDate[1], $tempDate[2], $tempDate[0]);
}
Test
checkmydate('2015-12-01'); //true
checkmydate('2015-14-04'); //false
Determine if string is a date, even if string is a non-standard format
(strtotime doesn't accept any custom format)
<?php
function validateDateTime($dateStr, $format)
{
date_default_timezone_set('UTC');
$date = DateTime::createFromFormat($format, $dateStr);
return $date && ($date->format($format) === $dateStr);
}
// These return true
validateDateTime('2001-03-10 17:16:18', 'Y-m-d H:i:s');
validateDateTime('2001-03-10', 'Y-m-d');
validateDateTime('2001', 'Y');
validateDateTime('Mon', 'D');
validateDateTime('March 10, 2001, 5:16 pm', 'F j, Y, g:i a');
validateDateTime('March 10, 2001, 5:16 pm', 'F j, Y, g:i a');
validateDateTime('03.10.01', 'm.d.y');
validateDateTime('10, 3, 2001', 'j, n, Y');
validateDateTime('20010310', 'Ymd');
validateDateTime('05-16-18, 10-03-01', 'h-i-s, j-m-y');
validateDateTime('Monday 8th of August 2005 03:12:46 PM', 'l jS \of F Y h:i:s A');
validateDateTime('Wed, 25 Sep 2013 15:28:57', 'D, d M Y H:i:s');
validateDateTime('17:03:18 is the time', 'H:m:s \i\s \t\h\e \t\i\m\e');
validateDateTime('17:16:18', 'H:i:s');
// These return false
validateDateTime('2001-03-10 17:16:18', 'Y-m-D H:i:s');
validateDateTime('2001', 'm');
validateDateTime('Mon', 'D-m-y');
validateDateTime('Mon', 'D-m-y');
validateDateTime('2001-13-04', 'Y-m-d');
This option is not only simple but also accepts almost any format, although with non-standard formats it can be buggy.
$timestamp = strtotime($date);
return $timestamp ? $date : null;
The easiest way to check if given date is valid probably converting it to unixtime using strtotime, formatting it to the given date's format, then comparing it:
function isValidDate($date) {
return date('Y-m-d', strtotime($date)) === $date;
}
Of course you can use regular expression to check for validness, but it will be limited to given format, every time you will have to edit it to satisfy another formats, and also it will be more than required. Built-in functions is the best way (in most cases) to achieve jobs.
You can also Parse the date for month date and year and then you can use the PHP function checkdate() which you can read about here: http://php.net/manual/en/function.checkdate.php
You can also try this one:
$date="2013-13-01";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date))
{
echo 'Date is valid';
}else{
echo 'Date is invalid';
}
I'm afraid that most voted solution (https://stackoverflow.com/a/19271434/3283279) is not working properly. The fourth test case (var_dump(validateDate('2012-2-25')); // false) is wrong. The date is correct, because it corresponds to the format - the m allows a month with or without leading zero (see: http://php.net/manual/en/datetime.createfromformat.php). Therefore a date 2012-2-25 is in format Y-m-d and the test case must be true not false.
I believe that better solution is to test possible error as follows:
function validateDate($date, $format = 'Y-m-d') {
DateTime::createFromFormat($format, $date);
$errors = DateTime::getLastErrors();
return $errors['warning_count'] === 0 && $errors['error_count'] === 0;
}
Accordling with cl-sah's answer, but this sound better, shorter...
function checkmydate($date) {
$tempDate = explode('-', $date);
return checkdate($tempDate[1], $tempDate[2], $tempDate[0]);
}
Test
checkmydate('2015-12-01');//true
checkmydate('2015-14-04');//false
I have this thing that, even with PHP, I like to find functional solutions. So, for example, the answer given by #migli is really a good one, highly flexible and elegant.
But it has a problem: what if you need to validate a lot of DateTime strings with the same format? You would have to repeat the format all over the place, what goes against the DRY principle. We could put the format in a constant, but still, we would have to pass the constant as an argument to every function call.
But fear no more! We can use currying to our rescue! PHP doesn't make this task pleasant, but it's still possible to implement currying with PHP:
<?php
function validateDateTime($format)
{
return function($dateStr) use ($format) {
$date = DateTime::createFromFormat($format, $dateStr);
return $date && $date->format($format) === $dateStr;
};
}
So, what we just did? Basically we wrapped the function body in an anonymous and returned such function instead. We can call the validation function like this:
validateDateTime('Y-m-d H:i:s')('2017-02-06 17:07:11'); // true
Yeah, not a big difference... but the real power comes from the partially applied function, made possible by currying:
// Get a partially applied function
$validate = validateDateTime('Y-m-d H:i:s');
// Now you can use it everywhere, without repeating the format!
$validate('2017-02-06 17:09:31'); // true
$validate('1999-03-31 07:07:07'); // true
$validate('13-2-4 3:2:45'); // false
Functional programming FTW!
How about this one?
We simply use a try-catch block.
$dateTime = 'an invalid datetime';
try {
$dateTimeObject = new DateTime($dateTime);
} catch (Exception $exc) {
echo 'Do something with an invalid DateTime';
}
This approach is not limited to only one date/time format, and you don't need to define any function.
Validate with checkdate function:
$date = '2019-02-30';
$date_parts = explode( '-', $date );
if(checkdate( $date_parts[1], $date_parts[2], $date_parts[0] )){
//date is valid
}else{
//date is invalid
}
Tested Regex solution:
function isValidDate($date)
{
if (preg_match("/^(((((1[26]|2[048])00)|[12]\d([2468][048]|[13579][26]|0[48]))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|[12]\d))))|((([12]\d([02468][1235679]|[13579][01345789]))|((1[1345789]|2[1235679])00))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|1\d|2[0-8])))))$/", $date)) {
return $date;
}
return null;
}
This will return null if the date is invalid or is not yyyy-mm-dd format, otherwise it will return the date.
/*********************************************************************************
Returns TRUE if the input parameter is a valid date string in "YYYY-MM-DD" format (aka "MySQL date format")
The date separator can be only the '-' character.
*********************************************************************************/
function isMysqlDate($yyyymmdd)
{
return checkdate(substr($yyyymmdd, 5, 2), substr($yyyymmdd, 8), substr($yyyymmdd, 0, 4))
&& (substr($yyyymmdd, 4, 1) === '-')
&& (substr($yyyymmdd, 7, 1) === '-');
}
To add onto the accepted answer, you can further check for a valid date or DateTime by checking if the formatted date is an instanceof DateTime.
$date = DateTime::createFromFormat('Ymd', $value);
$is_datetime = ($date instanceof DateTime);
$is_valid_datetime_format = $is_datetime
? ($date->format('Ymd') === $value)
: false;
if (!$is_datetime || !$is_valid_datetime_format) {
// Not a valid date.
return false;
}
This will catch any values that are not a DateTime such as random strings or an invalid date such as 20202020.
/**** date check is a recursive function. it's need 3 argument
MONTH,DAY,YEAR. ******/
$always_valid_date = $this->date_check($month,$day,$year);
private function date_check($month,$day,$year){
/** checkdate() is a php function that check a date is valid
or not. if valid date it's return true else false. **/
$status = checkdate($month,$day,$year);
if($status == true){
$always_valid_date = $year . '-' . $month . '-' . $day;
return $always_valid_date;
}else{
$day = ($day - 1);
/**recursive call**/
return $this->date_check($month,$day,$year);
}
}
Try and let me know it works for me
$date = \DateTime::createFromFormat('d/m/Y', $dataRowValue);
if (!empty($date)) {
//Your logic
}else{
//Error
}
if you pass any alpha or alphanumberic values it will give you the empty value in return
Regex solution
function verify_date($date){
/* correct format = "2012-09-15 11:23:32" or "2012-09-15"*/
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])( (0[0-9]|[1-2][0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]))?$/",$date)) {
return true;
} else {
die("Wrong date format: it should be '2012-09-15 11:23:32' or '2012-09-15', date received is: ".$date);
}
}
Try this:
$myday = '2022-1-30';
if (($timestamp = strtotime($myday)) === false) {
echo 'The string ('.$myday.') is not date';
} else {
echo 'The string ('.$myday.') is date = ' . date('l dS \o\f F Y h:i:s A', $timestamp);
}
Give this a try:
$date = "2017-10-01";
function date_checker($input,$devider){
$output = false;
$input = explode($devider, $input);
$year = $input[0];
$month = $input[1];
$day = $input[2];
if (is_numeric($year) && is_numeric($month) && is_numeric($day)) {
if (strlen($year) == 4 && strlen($month) == 2 && strlen($day) == 2) {
$output = true;
}
}
return $output;
}
if (date_checker($date, '-')) {
echo "The function is working";
}else {
echo "The function isNOT working";
}

How to check if 2 different format date strings is a valid dates?

I need to check if 2 different format date strings is a valid dates. The formats are: YYYY-MM-DD and YYYY.MM.DD. I found just only one date string format validation, like so:
function validateDate($date)
{
$d = DateTime::createFromFormat('Y-m-d', $date);
return $d && $d->format('Y-m-d') == $date;
}
function was copied from this answer or php.net
But how about two date formats validation? How to solve it? Thanks for any help
Try the following for both:
$date="2017-09-11";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
echo true;
} else {
echo false;
}
$date="2017.10.22";
if (preg_match("/^[0-9]{4}.(0[1-9]|1[0-2]).(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
echo true;
} else {
echo false;
}
It uses regex to check if the format is valid or not.
OR
$date="2017-09-11";
$dt = DateTime::createFromFormat("Y-m-d", $date);
echo $dt !== false && !array_sum($dt->getLastErrors());
$date="2017.10.22";
$dt = DateTime::createFromFormat("Y.m.d", $date);
echo $dt !== false && !array_sum($dt->getLastErrors());
It uses DateTime to check the date against both formats.
Edit: While both are decent solutions, benchmarks show that in this case, preg_match is considerably faster than DateTime. https://3v4l.org/H8C73
Copy the original function, where you specify format as 2nd parameter, and then run function twice; as already mentioned in the comments.
function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
$isValid = validDate($date, 'Y-m-d') || validDate($date, 'Y.m.d');
function was copied from this answer or php.net

Versatile format for date validations?

I am looking for a versatile method for validating dates.
What I want, is some way of achieving validation rules such as:
First monday of month
monday or friday
Fourth day of month
Is there anything that can handle such requirements? Or does anyone have any pointers as to how i could achieve this?
Edit: I appreciate the answers, but heres what i ended up using for now:
Thanks for all the answers. What i actually ended up using (At least till i find the perfect solution) is roughly this function:
function isDateValid($rule,$date){
$DATE = (int) $date;
/* Make it safe for eval */
preg_match_all('/:\w:|&&|\|\||<|=|>|!|\(|\)|\d+/i',$rule,$filteredRule);
$rule = implode('',$filteredRule[0]);
//replace rule notations
$f = function($str){
return "date('$str[1]',\$DATE)";
};
//Or if php < 5.3: $f = create_function('$str', 'return "date(\'$str[1]\',\$DATE)";');
$rule = preg_replace_callback('/:(\w):/i', $f, $rule);
//Evaluate expression
return #eval("return $rule;");
}
//Example of usage testing first of month:
var_dump( isDateValid(':d:==1',time()) );
This lets me handle all the requirements i have in, well PHP code. The people who use this is very limited (<5 persons) and most of them have some degree of php experience. Now i can make rules such as:
first monday of month : :w:==1 && :d: <= 7
monday or friday : :w:==1 || :w:==5
Fourth day of month : :d: == 4
I would appreciate an even better more flexible solution or feedback regarding security (Can this be injected with mallicious php code?)
I would define a set of rules something like:
$ordinals = array("First", "Second"...);
$days = array("Monday", "Tuesday"...);
$rules = array(
array("%1 %2 of month", $ordinals, $days),
array("%1 or %1", $days),
etc
)
Then, do a foreach(rule) and generate all possible allowed strings
$alloweds = array();
foreach($rules as $rule){
$str = $rule[0];
for($i = 1; $i < sizeof($rule); $i++){
for($a = 0; $a < sizeof($rule[$i]); $a++){
$alloweds[] = str_replace($str, "%$i", $rule[$i][$a]);
}
}
}
Then do a foreach(allowed string) compare to what we have
foreach($alloweds as $allowed){
if(preg_match($allowed, $input){
//its valid
}
}
you could set up your rules to be quite complicated - however its worth noting that the more complex they are the longer this will take - in its current form its clearly an exponential time algorithm, but its a starting point.
Not the most versatile solution, but this actually does EXACTLY what you want to do:
Class:
<?php
class validate {
private static $month;
private static $year;
private static $day;
private static $day_literal;
function __construct() {
self::$year = date("Y");
self::$month = date("m");
self::$day = date("d");
self::$day_literal = date("l");
}
public function firstInMonth($required,$day,$month,$year) {
$firstday;
for ($i = 1; $i < 31; $i++) {
if (checkdate($month,$i,$year)) {
if (trim(strtolower(date("l",mktime(0,0,0,$month,$i,$year)))) == trim(strtolower(trim($required)))) {
$firstday = $i;
break;
}
else {
continue;
}
}
}
if (strtotime(date("Y-m-d",mktime(0,0,0,$month,$i,$year))) == strtotime(date("Y-m-d",mktime(0,0,0,$month,$day,$year)))) {
return "{$year}/{$month}/{$day} is the first {$required} of the month.";
}
else {
return "Nope.";
}
}
public function checkDayLiteral($literal,$day,$month,$year) {
if (trim(strtolower(date("l",mktime(0,0,0,$month,$day,$year)))) == trim(strtolower($literal))) {
return true;
}
else {
return false;
}
}
public function dayPosition($day,$month,$year) {
return date("jS",mktime(0,0,0,$month,$day,$year));
}
}
?>
I don't know what's the purpose of your checking, so I've also implemented the construct method to compile the "today" values, which are NOT used in the code. If you need them, feel free to call them using self::
How to use:
<?php
$validate = new validate;
echo $validate->firstInMonth("Monday",2,9,2013);
echo "<br />";
echo $validate->checkDayLiteral("Monday",2,9,2013);
echo "<br />";
echo $validate->dayPosition(2,9,2013);
?>
Change the class according to WHAT you need to get (I mean: edit the returns I've set).
In this case, the above code returns:
2013/9/2 is the first Monday of the month.
1
2nd
while this:
echo $validate->firstInMonth("Tuesday",2,9,2013);
echo "<br />";
echo $validate->checkDayLiteral("Wednesday",15,9,2013);
echo "<br />";
echo $validate->dayPosition(24,9,2013);
returns this:
Nope.
24th
Of course, this is not as versatile as whatever you want, but this works.
Hope this helps.
If it does not do exactly what you want I'm sorry, just take this as an idea, I think it's pretty clear how you can get whatever information you want from this code.

strtotime function for hours more than 24

Hello I am trying to compare a time during with Hours:Minutes:Seconds format using strtotime function but there is a probleme if Hours is more than 24
PHP Code:
$time_duration_1 = '102:44:18';
$time_duration_2 = '87:42:19';
if(strtotime($time_duration_1) > strtotime($time_duration_2))
{
return true;
}else{
return false;
}
Is there any handy function for that kind of situation? or shall I do the checking manually hard coded?
Thanks in advance
Make a function to calculate your hours, minutes and seconds:
function formatHours($time){
$date = explode(':', $time);
return ($date[0]*60*60)+($date[1]*60)+$date[2];
}
if(formatHours($time_duration_1) > formatHours($time_duration_2)){
// ......
The way I'd solve that would be with mktime:
function getTimeFromString($time){
$time = explode(':', $time);
return mktime($time[0], $time[1], $time[2]);
}
if(getTimeFromString($time_duration_1) > getTimeFromString($time_duration_2)){

Checking if date is weekend PHP

This function seems to only return false. Are any of you getting the same? I'm sure I'm overlooking something, however, fresh eyes and all that ...
function isweekend($date){
$date = strtotime($date);
$date = date("l", $date);
$date = strtolower($date);
echo $date;
if($date == "saturday" || $date == "sunday") {
return "true";
} else {
return "false";
}
}
I call the function using the following:
$isthisaweekend = isweekend('2011-01-01');
If you have PHP >= 5.1:
function isWeekend($date) {
return (date('N', strtotime($date)) >= 6);
}
otherwise:
function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}
Another way is to use the DateTime class, this way you can also specify the timezone.
Note: PHP 5.3 or higher.
// For the current date
function isTodayWeekend() {
$currentDate = new DateTime("now", new DateTimeZone("Europe/Amsterdam"));
return $currentDate->format('N') >= 6;
}
If you need to be able to check a certain date string, you can use DateTime::createFromFormat
function isWeekend($date) {
$inputDate = DateTime::createFromFormat("d-m-Y", $date, new DateTimeZone("Europe/Amsterdam"));
return $inputDate->format('N') >= 6;
}
The beauty of this way is that you can specify the timezone without changing the timezone globally in PHP, which might cause side-effects in other scripts (for ex. Wordpress).
If you're using PHP 5.5 or PHP 7 above, you may want to use:
function isTodayWeekend() {
return in_array(date("l"), ["Saturday", "Sunday"]);
}
and it will return "true" if today is weekend and "false" if not.
Here:
function isweekend($year, $month, $day)
{
$time = mktime(0, 0, 0, $month, $day, $year);
$weekday = date('w', $time);
return ($weekday == 0 || $weekday == 6);
}
The working version of your code (from the errors pointed out by BoltClock):
<?php
$date = '2011-01-01';
$timestamp = strtotime($date);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
echo "true";
} else {
echo "false";
}
?>
The stray "{" is difficult to see, especially without a decent PHP editor (in my case). So I post the corrected version here.
For guys like me, who aren't minimalistic, there is a PECL extension called "intl".
I use it for idn conversion since it works way better than the "idn" extension and some other n1 classes like "IntlDateFormatter".
Well, what I want to say is, the "intl" extension has a class called "IntlCalendar" which can handle many international countries (e.g. in Saudi Arabia, sunday is not a weekend day). The IntlCalendar has a method IntlCalendar::isWeekend for that. Maybe you guys give it a shot, I like that "it works for almost every country" fact on these intl-classes.
EDIT: Not quite sure but since PHP 5.5.0, the intl extension is bundled with PHP (--enable-intl).
This works for me and is reusable.
function isThisDayAWeekend($date) {
$timestamp = strtotime($date);
$weekday= date("l", $timestamp );
if ($weekday =="Saturday" OR $weekday =="Sunday") { return true; }
else {return false; }
}
As opposed to testing the explicit day of the week string or number, you can also test using the relative date this weekday of the supplied date.
A direct comparison between the values is not possible without a workaround, as the use of weekday resets the time of the supplied date to 00:00:00.0000.
DateTimeInterface objects
$date->setTime(0, 0, 0) != $date->modify('this weekday');
DateTimeInterface Method
A simple method to implement to ensure the supplied date object is not changed.
function isWeekend(DateTimeInterface $date): bool
{
if ($date instanceof DateTime) {
$date = DateTimeImmutable::createFromMutable($date);
}
return $date->setTime(0,0,0) != $date->modify('this weekday');
}
isWeekend(new DateTimeImmutable('Sunday')); //true
strtotime method
With strtotime you can compare with the date('Yz') format. If the Yz value changes between the supplied date and this weekday, the supplied date is not a weekday.
function isWeekend(string $date): bool
{
return date('Yz', strtotime($dateValue)) != date('Yz', strtotime($dateValue . ' this weekday'));
}
isWeekend('Sunday'); //true
Example
https://3v4l.org/TSAVi
$sunday = new DateTimeImmutable('Sunday');
foreach (new DatePeriod($sunday, new DateInterval('P1D'), 6) as $date) {
echo $date->format('D') . ' is' . (isWeekend($date) ? '' : ' not') . ' a weekend';
}
Result
Sun is a weekend
Mon is not a weekend
Tue is not a weekend
Wed is not a weekend
Thu is not a weekend
Fri is not a weekend
Sat is a weekend

Categories