I posted a question last night that turned out to not be the problem. Digging around, I have discovered that the below code is giving me a headche. I had this working, but now for some reason I get no output. When I var_dump the function that gives me the $finishmins value, it outputs everything correctly until the point where it has to search the array (as below). After this it shows NULL. I was originally using strpos to find out if it started with a zero, then stripped said zero to match to the array, but when it stopped working, I tried the below approach to reduce code.
The point of the code is to convert minutes in time to minutes in decimal notation. I.e. 1 minute = 02, thus 12:01 = 12.02.
$finishmins = '01';
$finishmins = $minarray[$finishmins];
$minarray = array(
00 => '00',
01 => '02',
02 => '03',
03 => '05',
04 => '07',
05 => '08',
06 => '10',
07 => '12',
08 => '13',
09 => '15',
10 => '17',
'18',
// Array continues to 59 => '98'
);
echo $finishmins;
I have pasted the complete code here: http://codepad.org/EUW3n7AB and still can't seem to find the problem.
There are two issues here:
Array indices behave differently between strings and numbers,
Variable scope of $minarray.
$arr[01] and $arr['01'] are not the same thing, so you should be more explicit; in your case you can just leave the array numerically indexed, i.e.:
$minarray = array('00', '02', '03', '05', ...);
Then, you use an (int) cast on the given minutes:
$finishmins = $minarray[(int)$finishmins];
You can solve the second issue by passing the array as a function argument:
function finishtime($minarray, $finish)
Then calling it like so:
echo finishtime($minarray, '12:01');
You have to use the keyword global in your function when referring to the $minarray variable:
function finishtime($finish) {
global $minarray;
$finishx = explode(':', $finish);
$finishhours = $finishx[0];
$finishmins = $finishx[1];
$finishmins;
var_dump($finishmins);
$finishmins = $minarray[$finishmins];
var_dump($finishmins);
$finishtime = $finishhours . '.' . $finishmins;
return $finishtime;
}
Related
The following code works if I manually type in every day for every single month in each hard coded array.
I then loop through the arrays for a match and if I find it, I return the first index and the last index value of that array. These are the pay period start and end dates later to be used with mysql select queries.
// MOUNTAIN DAYLIGHT SAVINGS TIME
date_default_timezone_set('MST7MDT');
// = PHP Default TimeZone
//print 'MOUNTAIN DAYLIGHT SAVINGS TIME';
//print '<p>';
// = MySQL CURRDATE() in MySQL DATETIME Format.
$php_current_date = date('Y-m-d');
// 2019 Pay Periods - MONTHLY
$parent_array = array(
1 => array('2019-01-01','2019-01-31'),
2 => array('2019-02-01','2019-02-28'),
3 => array('2019-03-01','2019-03-31'),
4 => array('2019-04-01','2019-04-30'),
5 => array('2019-05-01','2019-05-31'),
6 => array('2019-06-01','2019-06-30'),
7 => array('2019-07-01','2019-07-31'),
8 => array('2019-08-01','2019-08-31'),
9 => array('2019-09-01','2019-09-30'),
10 => array('2019-10-01','2019-10-31'),
11 => array('2019-11-01','2019-11-30'),
12 => array('2019-12-01','2019-12-31'),
13 => array('2020-01-01','2020-01-31'),
14 => array('2020-02-01','2020-02-29'),
15 => array('2020-03-01','2020-03-31'),
16 => array('2020-04-01','2020-04-30'),
17 => array('2020-05-01','2020-05-31'),
18 => array('2020-06-01','2020-06-30'),
19 => array('2020-07-01','2020-07-31'),
20 => array('2020-08-01','2020-08-31'),
21 => array('2020-09-01','2020-09-30'),
22 => array('2020-10-01','2020-10-31'),
23 => array('2020-11-01','2020-11-30'),
24 => array('2020-12-01','2020-12-31')
);
$current_pay_period_start = '';
$current_pay_period_end = '';
// For each child Array of date Strings inside parent Array of arrays...
foreach($parent_array as $child_array){
// Speculate the variable name as $result_found while searching each child Array of date Strings
// for the Current date in *Mountain Daylight Savings Time
$result_found = in_array($php_current_date, $child_array);
// if we have a match...
if ($result_found) {
// GET LEFT-MOST index and assign it to a variable.
$current_pay_period_start = current($child_array);
// GET RIGHT-MOST index and assign it to another variable.
$current_pay_period_end = end($child_array);
// Add a day for mysql query logic...
// because mysql uses < instead of =< for comparison in the query the follows...
$current_pay_period_end = date('Y-m-d', strtotime($current_pay_period_end . ' + 1 days'));
/*
Following Works ONLY on direct access.
Debug Only.
Eg. localhost/folder/filename.php
*/
print 'Php Current Date: ' . $php_current_date;
print '<br>';
print 'Current Pay Period Start: ' . $current_pay_period_start;
print '<br>';
print 'Current Pay Period End: ' . $current_pay_period_end;
exit;
}
}
I have tried to implement the solution below but, I keep getting errors related to me not being able to compare date strings... It seems I have learned to find date strings in an array of arrays but they aren't really dates as far as php is concerned.
/**
* #param DateTime $date Date that is to be checked if it falls between $startDate and $endDate
* #param DateTime $startDate Date should be after this date to return true
* #param DateTime $endDate Date should be before this date to return true
* return bool
*/
function isDateBetweenDates(DateTime $date, DateTime $startDate, DateTime $endDate) {
return $date > $startDate && $date < $endDate;
}
$fromUser = new DateTime("2012-03-01");
$startDate = new DateTime("2012-02-01 00:00:00");
$endDate = new DateTime("2012-04-30 23:59:59");
echo isDateBetweenDates($fromUser, $startDate, $endDate);
Here's how I try to call it and get the error...
isDateBetweenDates($php_current_date, $current_pay_period_start, $current_pay_period_end);
I have wrote the following for you, that compares the two dates from your array. Hopefully it will help!
$php_current_date = date('Y-m-d');
$parent_array = array(
1 => array('2019-01-01','2019-01-31'),
2 => array('2019-02-01','2019-02-28'),
3 => array('2019-03-01','2019-03-31'),
4 => array('2019-04-01','2019-04-30'),
5 => array('2019-05-01','2019-05-31'),
6 => array('2019-06-01','2019-06-30'),
7 => array('2019-07-01','2019-07-31'),
8 => array('2019-08-01','2019-08-31'),
9 => array('2019-09-01','2019-09-30'),
10 => array('2019-10-01','2019-10-31'),
11 => array('2019-11-01','2019-11-30'),
12 => array('2019-12-01','2019-12-31'),
13 => array('2020-01-01','2020-01-31'),
14 => array('2020-02-01','2020-02-29'),
15 => array('2020-03-01','2020-03-31'),
16 => array('2020-04-01','2020-04-30'),
17 => array('2020-05-01','2020-05-31'),
18 => array('2020-06-01','2020-06-30'),
19 => array('2020-07-01','2020-07-31'),
20 => array('2020-08-01','2020-08-31'),
21 => array('2020-09-01','2020-09-30'),
22 => array('2020-10-01','2020-10-31'),
23 => array('2020-11-01','2020-11-30'),
24 => array('2020-12-01','2020-12-31')
);
foreach ($parent_array as $child_array) {
//compare dates using strtotime, did the conversion in the if statement to retain the original date format, for output if results are found.
if (strtotime($php_current_date) >= strtotime($child_array[0]) && strtotime($php_current_date) <= strtotime($child_array[1])) {
// match found...
$current_pay_period_start = $child_array[0];
$current_pay_period_end = $child_array[1];
print 'Php Current Date: ' . $php_current_date;
print '<br>';
print 'Current Pay Period Start: ' . $current_pay_period_start;
print '<br>';
print 'Current Pay Period End: ' . $current_pay_period_end;
exit;
}
}
I have tested it and the following is outputted:
Php Current Date: 2019-03-07
Current Pay Period Start: 2019-03-01
Current Pay Period End: 2019-03-31
Hopefully this will help!
$time = time();
$foo = array(
1448319600 => array(
array(
'foo' => 'bar'
),
array(
'bar' => 'foo'
)
),
1448578800 => array(
array(
'foo2' => 'bar2'
),
array(
'bar2' => 'foo2'
)
)
);
function bar($time, $foo) {
$count = 0;
do {
$count++;
$time = strtotime('+1 day', $time);
} while (isset($foo[$time]) === false);
return array(
'count' => $count,
'foo' => $foo[$time]
);
}
bar($time, $foo);
It's loading and loading and loading, because isset($foo[$time]) === false always seems to be true. I just can't find the error.
(Some more text to be able to submit. Some more text to be able to submit. Some more text to be able to submit.)
strtotime('+1day') is going to use "now" , e.g. 3:35pm as the base time, so you're doing the equivalent of
$now = time(); // 1448400979
$tomorrow = strtotime('+1 day', $now); // 1448487379
while(!isset($arr[$tomorrow])) { ... }
Since that value is highly unlikely to ever be in your array as a key, your loop never ends. You need to do a >= comparison or something, to check when your currently-being-considered date becomes larger than any key in your array.
At the start of your script you are setting time to the current time. Then in your do while loop you are incrementing the time by one day each time it loops. isset($foo[time]) === false will only return false and exit the loop when your time started at exactly 11pm sometime before Mon, 23 Nov 2015 23:00:00 +0000 or Thu, 26 Nov 2015 23:00:00 +0000.
An example being the timestamp while writing this post is 1448401349 If I add one day to it I get 1448487749.
You might want to look into rounding your timestamps to midnight to make sure you get collisions in your loop that allow it to exit. unix timestamp round to midnight
I need to format a date in my array - but the date in the array isn't saved as a datetime in a database or something like this.. I've got the dates from my server with cut them out.
So I need to work with preg_replace or with str_replace
What I've tried so far using str_replace:
$reverse_date = str_replace( '[', '' ,$reverse_date);
$reverse_date = str_replace( ']', '' ,$reverse_date);
$reverse_date = str_replace( '/', '.' ,$reverse_date);
but I don't want to use three lines for this.
If I print_r this, I will get : 12.Oct.2015:01:10:43 +0200
before it was looking like this : [12/Oct/2015:00:37:29 +0200]
so this is okay ! But I still don't want to use three lines for this, but I don't understand the preg_replace syntax
I want the following output :
12.Oct.2015(space)01:10:43 +0200
As you have said you were getting a date from an array within the following format
[12/Oct/2015:00:37:29 +0200]
So instead of using str_replace or preg_replace you can simply use DateTime::createFromFormat function of PHP like as
$date = DateTime::createFromFormat("[d/M/Y:H:i:s P]","[12/Oct/2015:00:37:29 +0200]");
echo $date->format('d.M.Y H:i:s P');//12.Oct.2015 00:37:29 +02:00
Demo
Use date_parse to disassemble the date and combine the parts to form your needed result:
[40] boris> $date_array = date_parse(" [12/Oct/2015:00:37:29 +0200] ");
// array(
// 'year' => 2015,
// 'month' => 10,
// 'day' => 12,
// 'hour' => 0,
// 'minute' => 37,
// 'second' => 29,
// 'fraction' => 0,
// 'warning_count' => 0,
// 'warnings' => array(
//
// ),
// 'error_count' => 2,
// 'errors' => array(
// 0 => 'Unexpected character',
// 27 => 'Unexpected character'
// ),
// 'is_localtime' => true,
// 'zone_type' => 1,
// 'zone' => -120,
// 'is_dst' => false
// )
You don't have the month as abbreviated string, but that is trivial to add via an associative array (array(1 => 'Jan', ..., 12 => 'Dec')), and you are on the safe side concerning the date-parsing stuff and future changes in your needs.
Ok I found out how to do it with preg_replace in one line, however I like the Uchiha answer with the date format more - even that he is not using the regex, this is probably the best way to go.
echo preg_replace(['~(?<=\d{4}:\d{2}):~', '~[\[]~', '~[\]]~', '~[\/]~g'],[' ', '', '', '.'],'[12/Oct/2015:00:37:29 +0200]');
12.Oct.2015:00 37:29 +0200
I know how to check for a value in an array, but how do I check for a value in an Array Iterator?
$array = new ArrayIterator(array(
'1QmRjtsw2UQ' => array('pubdate' => '26 Jun 15', 'alt' => '8 Year Old Beautifully Covers Thinking Out Loud', 'anchor text' => '8-yo \'Thinking Out Loud\''),
'eKqLaYrcf3A' => array('pubdate' => '25 Jun 15', 'alt' => 'Plane Lands On Truck', 'anchor text' => 'Plane Lands On Truck'),
));
I'm trying to check for the values such as 1QmRjtsw2UQ.
This does not work:
if(in_array('1QmRjtsw2UQ', $array));
why don't you use array_key_exists ?
if(array_key_exists('1QmRjtsw2UQ', $array))
{
// do something
}
Try this,
$array->offsetExists('1QmRjtsw2UQ');
I have a holiday class that contains a multidimensional array of holidays with different time periods that works basically like this:
$this->bgArray = array( 'background' => array(
array( 'name' => 'SuperBowl',
'start' => '06 Feb 2011',
'end' => '07 Feb 2011'),
array( 'name' => 'presidentsday2011',
'start' => '21 Feb 2011',
'end' => '22 Feb 2011'),
array( 'name' => 'marchmadness6',
'start' => '01 Mar 2011',
'end' => '02 Mar 2011')));
and when it is called I want to be able to select the right holiday and run subsequent code, currently I have that set up as:
foreach($holidays->bgArray['background'] AS $holiday)
{
if (($holidays->today >= strtotime($holiday['start']) && $holidays->today < strtotime($holiday['end']) || (isset($_REQUEST[$holiday['name']])) ))
{
$background = $holiday['name'];
}
}
$this->set_background($background);
this code is all working great but personally I feel like there is a better way to do this code than a foreach loop. Does anyone have any suggestions? Also, no I don't have access to a DB so I have to this all within PHP. Any help is appreciated, thanks guy.
foreach is fine for arrays. You might want to use unix timestamps directly in your array since always using strtotime isn't excactly the fastest way.
What you also could do is splitting the array up into months, having a subarray for each month. That way you don't have to iterate over all holidays but only the specific ones for the month to be displayed.