PHP add different values out of array alternately - php

Example :
00:00 22-03-2017, John Wilson
08:00 22-03-2017, Gemma Arterton
16:00 22-03-2017, Arnold Plank
00:00 22-03-2017, Timmy Brouwer
08:00 22-03-2017, John Wilson <- names repeating
16:00 22-03-2017, Gemma Arterton
I am building a shift system that generated a time and date for the next 30 days. I am trying to get different related names to getting echo'd behing the date but so far no luck.
The names are interactive meaning there could be only 1 aswell as 25 (so to speak).
This is my current call to function code:
$month = $shift->date_range(date("Y-m-d"), date('Y-m-d', strtotime("+30 days")), "+" . $shift_facility['facility_shift_duration'] . " hours", "Y/m/d H:i:s", $user_per_facility);
And this is my function itself:
public function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y', $users)
{
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while ($current <= $last) {
// Add date and time to array $dates
$dates[] = date($output_format, $current) . ';' . $users[0]["user_id"];
//check if we are still in range between the two dates
$current = strtotime($step, $current);
}
return $dates;
}
$users contains all the user data e.g:
array(2) {
[0]=>
array(5) {
["user_id"]=>
string(1) "3"
["user_alias"]=>
string(7) "ND12345"
["user_facility"]=>
string(2) "29"
["user_phone"]=>
string(5) "12345"
["user_name"]=>
string(9) "Jan steen"
}
[1]=>
array(5) {
["user_id"]=>
string(1) "7"
["user_alias"]=>
string(7) "ND68596"
["user_facility"]=>
string(2) "29"
["user_phone"]=>
string(11) "31115648597"
["user_name"]=>
string(8) "John Doe"
}
}
$users[0]["user_id"];
Only outputs the first name but I need them to alternate (look at the first example).
Does anyone have any idea that could point me in the right direction or help me out a bit?
Thanks in advance ^^
EDIT: More details
My function call is in a foreach:
foreach ($facility->getfacilities() as $shift_facility) {
$user_per_facility = $user->getusersbyFacilityId($shift_facility['facility_id']);
if (isset($user_per_facility[0]["user_id"])) {
//if shift count > dan db results generate new day
$month = $shift->date_range(date("Y-m-d"), date('Y-m-d', strtotime("+30 days")),
"+" . $shift_facility['facility_shift_duration'] . " hours", "Y/m/d H:i:s", $user_per_facility);
}
}

You're sending an entire array of $users to the date_range() method, where you should only be sending one entity of that array (e.g. a single user) to that method.
You're calling $users[0] from within that method, which will always reference the first (zeroth) element from that array, not the specific one you're looking for.
The issue starts from this block:
if (isset($user_per_facility[0]["user_id"])) {
//if shift count > dan db results generate new day
$month = $shift->date_range(date("Y-m-d"), date('Y-m-d', strtotime("+30 days")),
"+" . $shift_facility['facility_shift_duration'] . " hours", "Y/m/d H:i:s", $user_per_facility);
}
That only checks if the user_id is set in the first array element. You need to loop through the users array as well:
foreach ($facility->getfacilities() as $shift_facility) {
$user_per_facility = $user->getusersbyFacilityId($shift_facility['facility_id']);
// Make sure $users_per_facility can be iterated over
if (count($users_per_facility) > 0 && is_array($users_per_facility))
foreach ($users_per_facility as $u) {
if (isset($u["user_id"])) {
//if shift count > dan db results generate new day
$month = $shift->date_range(date("Y-m-d"), date('Y-m-d', strtotime("+30 days")),
"+" . $shift_facility['facility_shift_duration'] . " hours", "Y/m/d H:i:s", $u);
}
}
}
}
Then modify your date_range() method to only take a single user element, not the entire array. For clarification, I changed $users to $user because there is only one "user" being sent to that method:
public function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y', $user) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while ($current <= $last) {
// Add date and time to array $dates
$dates[] = date($output_format, $current) . ';' . $user["user_id"];
//check if we are still in range between the two dates
$current = strtotime($step, $current);
}
return $dates;
}
I would also suggest that you re-order the argument list for the date_range method. I'd suggest putting $user first:
public function date_range($user, $first, $last, $step = '+1 day', $output_format = 'd/m/Y')
Also, it's good convention to give your methods (functions) names with a verb, since they "do" something. In this case, make_date_range. Since I can see this is in a class (having the public keyword), it's a good habit to make your method names camelCase, e.g. makeDateRange(), while keeping your variables with underscores. Not required by any means, but it's good practice.
Hope this helps.

In case people stumble on the same issue:
I fixed this issue by creating an variable $i and if it exceeded the size of var $users had it set back to value 0.
(aswell as counting up for each succesfull row)
$i = 0;
if ($i > count($users)-1) {
$i = 0;
}
// Add date and time to array $dates
$dates[] = date($output_format, $current) . ';' . $users[$i]["user_id"];
//check if we are still in range between the two dates
$current = strtotime($step, $current);
$i++;

Related

php datetime multiple comparisons not working as expected but work individually?

I'm having issues with my if statement in that both conditions give the expected result individually but when combined so that when both are true the statement breaks.
The idea is to calculate the start date before Christmas so I can show a message about this. My logic was that by ensuring the start date is after October 1st and the end date before December 24th this message will always appear at the correct time give our start and end date schedule.
My code so far is below, there is also a dump below each variable .
foreach ( $events as $event ) {
$start = date("d-m-Y", strtotime($event->EventStartDate));
// string(10) "26-10-2018"
$end = date("d-m-Y", strtotime($event->EventEndDate));
// string(10) "20-12-2018"
echo '<div id="prod-dates">';
echo '<p>Order before ' . $start . '</p>';
echo '<p>Estimated Delivery ' . $end . '</p>';
echo '</div>';
//
//EVERYTHING IS FINE UNTIL HERE...
//
$this_christmas = date('Y-12-24');
// string(10) "2018-12-24"
$startx = date("Y-m-d", strtotime($event->EventStartDate));
// string(10) "2018-10-26"
$endx = date("Y-m-d", strtotime($event->EventEndDate));
// string(10) "2018-12-20"
$note_start = date('Y-11-01');
// string(10) "2018-11-01
if ( $startx >= $note_start || $endx <= $this_christmas ) {
echo 'This is your last deadline for <span>Christmas Delivery</span>';
}
}
Any help appreciated.
Thanks
You say "October 1st" in your description but in your code $note_start is set to November (2018-11-01).
You want to ensure "the start date is after October 1st and the end date before December 24th" but you check whether either condition is true with if ( $startx >= $note_start || $endx <= $this_christmas ). Replace the or (||) with and (&&).
date function returns a string value, so your dates are compared as strings.
Better use values that return strtotime to compare your dates
Sample:
$start = strtotime($event->EventStartDate);
$end = strtotime($event->EventEndDate);
//$note_start in time format
if ( $startx >= $note_start || $endx <= $this_christmas ) {
//echo
}

PHP days difference calculation error

I have some PHP code to calculate the number of days between two specific dates. The difference should not count Sundays and Saturdays. Also, I have an array of dates, which includes holidays, which also need to be skipped.
I gave the starting date as 01-05-2015 and ending date as 01-06-2015. I gave the entire days in the month of may as array. Thus the difference should be 1 day. But I am getting the output as 7. What is the problem? Here is the code.
function dateRange($first, $last) {
$dates = array();
$current = strtotime($first);
$now = $current;
$last = strtotime($last);
while( $current <= $last ) {
if (date('w', $current) != 0){
$dates[] = date('d-m-Y', $current);
}
$current = strtotime('+1 day', $current);
}
unset($dates[0]);
return $dates;
}
$datea = "01-05-2015";
$date = "01-06-2015";
$hdsarray = array("1-05-2015","2-05-2015","4-05-2015","5-05-2015","7-05-2015","8-05-2015","9-05-2015","11-05-2015","12-05-2015","14-05-2015","15-05-2015","16-05-2015","18-05-2015","19-05-2015","21-05-2015","22-05-2015","23-05-2015","25-05-2015","26-05-2015","28-05-2015","29-05-2015","30-05-2015");
$datesarray = dateRange($datea, $date);
$result = array_diff($hdsarray,$datesarray);
$date_diff = sizeof($result);
echo $date_diff;
The only problem I can see is in the usage of array_diff, It actually includes the sat and sun which is excluded by dateRange function, if not found in holidays list.
Instead, you can pass your holiday dates in dateRange function, and filter over there.
function dateRange($first, $last, $excludeDates) {
$dates = array();
$current = strtotime($first);
$now = $current;
$last = strtotime($last);
while( $current <= $last ) {
if (date('w', $current) != 0 && date('w', $current) != 6 && !in_array(date('j-m-Y', $current), $excludeDates)){
$dates[] = date('d-m-Y', $current);
}
$current = strtotime('+1 day', $current);
}
return $dates;
}
$datea = "01-05-2015";
$date = "01-06-2015";
$hdsarray = array("1-05-2015","2-05-2015","4-05-2015","5-05-2015","7-05-2015","8-05-2015","9-05-2015","11-05-2015","12-05-2015","14-05-2015","15-05-2015","16-05-2015","18-05-2015","19-05-2015","21-05-2015","22-05-2015","23-05-2015","25-05-2015","26-05-2015","28-05-2015","29-05-2015","30-05-2015");
$datesarray = dateRange($datea, $date, $hdsarray);print_r($datesarray);
Result:
Array
(
[0] => 06-05-2015
[1] => 13-05-2015
[2] => 20-05-2015
[3] => 27-05-2015
[4] => 01-06-2015
)
All the 5 dates come in the result, are not sat, sun, and also not there in holidays list.
It seems that there are several problems here. First, as pointed out by others the condition:
if (date('w', $current) != 0){
only checks for Sundays, if it should also include Saturday's it should be:
if (date('w', $current) != 0 && date('w', $current) != 6){
Secondly, it seems that the $hdsarray array does not contain all of the days in May. It seems that all of the Wednesdays are missing.
The third issue is that you are using array_diff on two arrays, one containing Dates and the other ones containing Strings. From the documentation:
Two elements are considered equal if and only if (string) $elem1 ===
(string) $elem2. In words: when the string representation is the same.
In your $hdsarray you are using "1-05-2015" to denote the first day of the month, while:
echo date('d-m-Y', strtotime("1-05-2015"));
results in "01-05-2015". You will need to add an additional 0 in $hdsarray for these dates or work with dates as well.
Last but not least, the current algorithm will not work correctly if the $hdsarray contains dates for a Saturday or Sunday, the result of array_diff will still contain these dates. Since you want to filter the result of daterange the array_filter function might be more suitable.
Despite an answer has already been provided, here is a little snippet with a class handling everything for you:
<?php
class dateRange {
protected $start, $end, $daysToExclude, $datesToExclude;
function __construct($dateStart, $dateEnd, $daysToExclude, $datesToExclude) {
$this->start = $dateStart;
$this->end = $dateEnd;
$this->daysToExclude = $daysToExclude;
$this->datesToExclude = $this->fixFormat($datesToExclude);
}
public function getRangeLength ($callback = null) {
$tmp = array();
$now = strtotime($this->start);
$to = strtotime($this->end);
while ( $now <= $to ) {
if (!in_array(date("w", $now), $this->daysToExclude)) {
$tmp[] = date('d-m-Y', $now);
}
$now = strtotime('+1 day', $now);
}
is_callable($callback) && call_user_func($callback, array_diff($tmp,$this->datesToExclude));
return count(array_diff($tmp,$this->datesToExclude));
}
private function fixFormat($el) {
if (!is_array($el)) {
return false;
}
else {
foreach ($el as &$value) {
$value = date("d-m-Y",strtotime($value));
}
return $el;
}
}
}
?>
I decided to keep your current logic (using date_diff), but I thought that, in the future, you may have your boss telling you "You know what? I don't want to have mondays aswell there" so, with the current system, you will have to edit your function manually and, perhaps, you won't remember anymore what you did.
The class above expects four parameters:
dateStart (d-m-Y format)
dateEnd (d-m-Y format)
daysToExclude (array with IDs of the days to exclude -> example array(0,6) to exclude saturdays and sundays).
datesToExclude (array with the dates to exclude, every format supported).
The class will automatically fix the datesToExclude array format in order to allow you to use date_diff.
Here is an example to use it, following your case:
<?php
$dateStart = "01-05-2015";
$dateEnd = "01-06-2015";
$daysToExclude = array(0,6);
$exclusions = array(
"1-05-2015",
"2-05-2015",
"4-05-2015",
"5-05-2015",
"7-05-2015",
"8-05-2015",
"9-05-2015",
"11-05-2015",
"12-05-2015",
"14-05-2015",
"15-05-2015",
"16-05-2015",
"18-05-2015",
"19-05-2015",
"21-05-2015",
"22-05-2015",
"23-05-2015",
"25-05-2015",
"26-05-2015",
"28-05-2015",
"29-05-2015",
"30-05-2015"
);
$dateRange = new dateRange($dateStart, $dateEnd, $daysToExclude, $exclusions);
echo $dateRange->getRangeLength();
?>
The code above outputs 5.
The function getRangeLength also accepts a callback and will return the array resulting of the date_diff operation, so you can also:
$dateRange->getRangeLength(function($res) {
echo "Literal output: <br />";
print_r($res);
echo "<br />count is: " . count($res);
});
The above outputs:
Literal output:
Array ( [3] => 06-05-2015 [8] => 13-05-2015 [13] => 20-05-2015 [18] => 27-05-2015 [21] => 01-06-2015 )
count is: 5
So if you later will need to remove mondays too, you will be able to easily do that by changing daysToExclude to array(0,1,6);
Hope this will be helpful to anyone else who will need this, despite a valid answer has already been posted.
Your original problem, in any case, was pretty much related to the array_diff function, which was NOT doing its job because of the fact that the date strings were not compatible, because "1-01-2015" is different from "01-01-2015", unless you first convert BOTH of them to times and then back to dates.
The code is fine (except that $nowis not used at all). The problem is the $hdsarray is wrong:
It should $hdsarray = array("01-05-2015", "02-05-2015", "04-05-2015", "05-05-2015", "07-05-2015", "08-05-2015", "09-05-2015",...);
date('d-m-Y', $current);will always return a leading 0 for all days between 1 and 9.
That's where the difference comes from.

For loop not incrementing the date upwards

I have the below code that is adding 4 dates to an array but the date remains the same for each value in the array.
It just gives me 4 values of 2014/01/24 instead of 2014/01/24, 2014/01/31, 2014/02/07, 2014/02/14
Thanks
$myArray = array();
// Number of days
$days = get_post_meta($post_id, 'wpcf-inc_recur_freq', true);
// cycle from 1st week's due date to the end of payment cycle
for($i = 1; $i <= 4; $i++) {
$myArray[] = date($mysqldate, strtotime("+" . $days . " days"));
}
Yes, it will give you four copies of the same date, because you're simply adding a fixed number of days to the current date each time.
I think you probably want to change your for loop to be:
for($i = 0; $i < 4; $i++) {
$myArray[] = date($mysqldate, strtotime("+" . ($days * $i) . " days"));
}
Note that I've changed the loop to run 0..3 instead of 1..4, on the assumption that the first date you want is today. If the first date you actually want is $days days beyond today, revert to using 1..4.
You can see this in action in the following PHP code:
date_default_timezone_set("EST");
$myArray = array();
$days = 7;
for($i = 0; $i < 4; $i++) {
$myArray[] = date("Y-m-d", strtotime("+" . ($days * $i) . " days"));
}
var_dump($myArray);
which outputs (using one of the online PHP executors):
array(4) {
[0]=> string(10) "2014-01-24"
[1]=> string(10) "2014-01-31"
[2]=> string(10) "2014-02-07"
[3]=> string(10) "2014-02-14"
}
However, it may be that your actual $days variable is set to zero (you seem to think it will be set to seven). A $days value of zero would cause all dates to be today even with the corrected code above, so I'd be checking that. One possibility for the value being set to zero is your unusual mixture of underscores and hyphens in the key you're looking up:
wpcf-inc_recur_freq
^ ^ ^
| | |
| +-----+------- underscore
+----------------- hyphen
I suspect you didn't quite press as hard on the SHIFT key as you should have when entering that first _ character :-)
You're not incrementing $days in the loop, so it will give the same value each time the loop is executed.
You are not incrementing $days in your loop.
$mysqldate = date('Y-m-d');
// Number of days
$days = get_post_meta($post_id, 'wpcf-inc_recur_freq', true);
// cycle from 1st week's due date to the end of payment cycle
for($i = 1; $i <= 4; $i++) {
$myArray[] = date($mysqldate, strtotime("+" . $i*$days . " days"));
}
var_dump($myArray);
Try this.
$mysqldate = date('Y-m-d');
// Number of days
$days = get_post_meta($post_id, 'wpcf-inc_recur_freq', true);
// cycle from 1st week's due date to the end of payment cycle
for($i = 1; $i <= 4; $i++) {
$myArray[$i] = date($mysqldate, strtotime("+" . $i*$days . " days"));
}
var_dump($myArray);

How to find date difference excluding specific dates in array - PHP

I know how to calculate date difference using PHP like;
$newdate = "01-03-2013";
$olddate = "01-06-2013";
$date_diff = abs(strtotime($olddate)-strtotime($newdate)) / 86400;
echo $date_diff;
But suppose, if I have some dates in an array like;
$datesarray = array(10-05-2013, 20-05-2013, 12-08-2013);
etc., holding some specific dates, is it possible to calculate date difference excluding the dates in array along with the Sundays, if they lie in between the start and end dates?
just loop through the $datesarray and check for each one if it's between the $olddate and $newdate. If so, increase a $counter variable (which starts at 0, obviously).
Then $date_diff - $counter will give you the expected result.
I would use the DateTime class in a custom function like this:
function dates_between(DateTime $start, DateTime $end, $format = 'm-d-Y') {
$date = $start;
$dates = array();
$oneDay = new DateInterval('P1D');
// push all dates between start and end to the result
while(($date = $date->add($oneDay)) < $end) {
$dates []= $date->format($format);
}
return $dates;
}
Example usage:
$now = new DateTime();
$nextWeek = new DateTime('+1 week');
var_dump(dates_between($now, $nextWeek));
Output:
array(6) {
[0] =>
string(10) "07-12-2013"
[1] =>
string(10) "07-13-2013"
[2] =>
string(10) "07-14-2013"
[3] =>
string(10) "07-15-2013"
[4] =>
string(10) "07-16-2013"
[5] =>
string(10) "07-17-2013"
}
The following script creates and array of timestamps from your array of UK dates and then calculates the max and min timestamps to calculate the days difference.
If the timestamp defaults to 0, it is not added to the timestamp array, avoiding huge results for one bad date defaulting to the epoch
I.e. When date is invalid or pre epoch 1/1/1970
<?php
$datesarray = array('10-05-2013', '20-05-2013', '12-08-2013');
$date_diff=0; // default for 0 or 1 dates
if( (is_array($datesarray)) && (sizeof($datesarray)>1) )
{
$timestampsarray=array();
reset($datesarray);
while(list($key,$value)=each($datesarray))
{
$timestamp=timestamp_from_UK($value);
if($timestamp!=0) $timestampsarray[$key]=$timestamp;
}
$date_diff = abs(max($timestampsarray)-min($timestampsarray)) / 86400;
}
echo $date_diff;
function timestamp_from_UK($ukdatetime)
{
// where PHP is processing UK dates d-m-y correctly
$ukdatetime=str_replace('/', '-', $ukdatetime);
if(date("d", strtotime("1-2-1970"))==1) return strtotime($ukdatetime);
// Fallback script for when PHP is NOT processing UK dates
$success=false;
if(!$success) $success=preg_match("/([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{2,4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})/", $ukdatetime, $matches);
if(!$success) $success=preg_match("/([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{2,4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})/", $ukdatetime, $matches);
if(!$success) $success=preg_match("/([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{2,4})/", $ukdatetime, $matches);
if(!$success) return 0;
// ensure all values are set - to avoid invalid offset
for($i=4;$i<=6;$i++)
{
if(!isset($matches[$i])) $matches[$i]=0;
}
// $matches[0] is the full matched string
return mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[1], $matches[3]);
}
?>

Function to determine wether the current datetime is within a user set date and time in PHP [duplicate]

This question already has answers here:
How to check if a date is in a given range?
(10 answers)
Finding whether time is in a defined range [duplicate]
(5 answers)
Closed 8 years ago.
I've been working at this code off and on for the past few days and can't figure it out.
What I need to do is return from a function either a 0 or 1 depending on if the current time is within the times set by a user. If the time and date is within a 4 value array set by the user, then return 1, if not, return 0. The user can set multiple arrays for multiple periods of times.
I've been trying to work with this code for a while:
functions.php:
function determineWoE($woe) {
$curDayWeek = date('N');
$curTime = date('H:i');
$amountWoE = count($woe['WoEDayTimes']); // Determine how many WoE times we have.
if ( $amountWoE == 0 ) {
return 0; // There are no WoE's set! WoE can't be on!
}
for ( $i=0; $i < $amountWoE; $i++ ) {
if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) { // Check the day of the week.
if ( $woe['WoEDayTimes'][$i][1] >= $curTime && $woe['WoEDayTimes'][$i][3] <= $curTime ) { // Check current time of day.
// WoE is active
return 1;
}
else {
// WoE is not active
return 0;
}
}
else {
// WoE is not active
return 0;
}
}
}
And...where the user sets as many periods of time for this feature that they want:
$woe = array( // Configuration options for WoE and times.
// -- WoE days and times --
// First parameter: Starding day 1=Monday / 2=Tuesday / 3=Wednesday / 4=Thursday / 5=Friday / 6=Saturday / 7=Sunday
// Second parameter: Starting hour in 24-hr format.
// Third paramter: Ending day (possible value is same or different as starting day).
// Fourth (final) parameter: Ending hour in 24-hr format.
'WoEDayTimes' => array(
array(6, '18:00', 6, '19:00'), // Example: Starts Saturday 6:00 PM and ends Saturday 7:00 PM
array(3, '14:00', 3, '15:00') // Example: Starts Wednesday 2:00 PM and ends Wednesday 3:00 PM
),
);
But, no matter what I do...the function determineWoE always returns 0.
Am I needing a foreach in the function instead of a for? How do I get determineWoE to return 1 if the time is within the user settable times?
Tried changing the for to a foreach:
foreach ( $woe['WoEDayTimes'] as $i ) {
And now I get error:
Warning: Illegal offset type in /var/www/jemstuff.com/htdocs/ero/functions.php on line 76
...which I have no idea why I would be getting that error. Line 76 is:
if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) { // Check the day of the week.
In functions.php
var_dump($woe)
array(2) { ["WhoOnline"]=> string(2) "no" ["WoEDayTimes"]=> array(2) { [0]=> array(4) { [0]=> int(6) [1]=> string(5) "18:00" [2]=> int(6) [3]=> string(5) "19:00" } [1]=> array(4) { [0]=> int(3) [1]=> string(5) "14:00" [2]=> int(3) [3]=> string(5) "15:00" } } }
Thanks for any help you can provide to me. :)
A couple minor points:
A foreach loop and a for loop would both work fine, but you might find the foreach more covenient, since you wouldn't have to count() the days/times to check for.
You should return boolean true or false instead of 1 or 0.
I'm not sure why you're getting that error, but the bigger problem I see is how you compare the times. You cast the string times to numeric types, and that won't convert entirely like you think it will. For example...
"14:00" < "14:59"
...Would be false, because it casts both strings to 14. Thus, the first string actually equals the second.
You might be better off converting the strings to Unix Timestamps (which are the seconds since 1/1/1970), and comparing those.
Here's a rough idea of how I would do it:
// Function to help get a timestamp, when only given a day and a time
// $today is the current integer day
// $str should be 'last <day>', 'next <day>', or 'today'
// $time should be a time in the form of hh:mm
function specialStrtotime($today, $day, $time) {
// An array to turn integer days into textual days
static $days = array(
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
);
// Determine if the day (this week) is in the past, future, or today
if ($day < $today) {
$str = 'last ' . $days[$day];
} else if ($day > $today) {
$str = 'next ' . $days[$day];
} else {
$str = 'today';
}
// Get the day, at 00:00
$r = strtotime($str);
// Add the amount of seconds the time represents
$time = explode(':', $time);
$r += ($time[0] * 3600) + ($time[1] * 60);
// Return the timestamp
return $;
}
// Your function, modified
function determineWoE($timeNow, $woe) {
$dayNow = (int) date('N', $timeNow);
foreach ($woe as $a) {
// Determine current day
// Determine the first timestamp
$timeFirst = specialStrtotime($dayNow, $a[0], $a[1]);
// Determine the second timestamp
$timeSecond = specialStrtotime($dayNow, $a[2], $a[3]);
// See if current time is within the two timestamps
if ($timeNow > $timeFirst && $timeNow < $timeSecond) {
return true;
}
}
return false;
}
// Example of usage
$timeNow = time();
if (determineWoE($timeNow, $woe['WoEDayTimes'])) {
echo 'Yes!';
} else {
echo 'No!';
}
Good luck!

Categories