Is there any ways to use DATE in PHP as array. I need to achieve something as below:
$date_array = (1-Jan => 'A', 2-Jan => 'B', 3-Jan => 'C', .... so on)
Is it possible?
Manually:
$array['1-Jan'] = 'A';
$array['2-Jan'] = 'B';
...
Or with a loop:
$array = array();
$currentDate = strtotime('2010-01-01');
$totalDays = 365;
for ($i=0; $i<$totalDays; $i++) {
$array[date('j-M', $currentDate)] = $i;
$currentDate = strtotime("+1 day", $currentDate);
}
The PHP manual specifies that only string and integer can be used as keys for assoc. arrays, but date returns a string, so they should be fine, but to retrive the values back would be a bit messy.
What about something like:
$arr = Array(
'A'=>date('Y-m-d', time()),
'B'=>date('Y-m-d', time()-(7 * 24 * 60 * 60))
);
echo in_array(date('Y-m-d'), $arr);
That echos "1".
My mistake, you would need to use array_search() instead of in_array() so you get the array key:
echo array_search(date('Y-m-d'), $arr);
Ouputs "A".
Related
I have several date(strtotime) in a Variable and want the first nearest date that is after the specified date(my date) with php. what do i do?
Variable:
$varD = "1481691600,1482642000,1482037200";
my date:
1481778000 => (2016-12-15)
several date(strtotime):
1481691600 => (2016-12-14)
1482642000 => (2016-12-25)
1482037200 => (2016-12-18) //result
result:
1482037200 => (2016-12-18)
$varD = "1481691600,1482037200,1482642000";
$myDate = "1481778000";
After you explode the string of timestamps ($varD), you can filter them and return the minimum value of the result. Here is one way to do that using array_filter and min.
$comp = function($x) use ($myDate) { return $x > $myDate; };
$firstDateAfterYours = min(array_filter(explode(',', $varD), $comp));
But if you already know that the timestamps in the string will be in ascending order, it will probably be faster not to convert the whole thing to an array and sort through it. You can use strtok to go through it piece by piece and just stop as soon as you get to a timestamp larger than your target.
$ts = strtok($varD, ',');
while ($ts !== false) {
$ts = strtok(',');
if ($ts > $myDate) break;
}
$firstDateAfterYours = $ts;
So i have a very simple snippet. I studied the in_array command and found it would be what i need.
However its not working?
I have tried several scenarios:
$this->item->tour_days is an array containing 1,2,3
test 1
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array,TRUE)) {
echo 'Tuesday';
}
test 2
$days = $this->item->tour_days;
$array = array($days);
if (in_array(2,$array)) {
echo 'Tuesday';
}
test 3
$days = $this->item->tour_days;
$array = array($days);
if (in_array('2',$array)) {
echo 'Tuesday';
}
I have tried to echo Tuesday where Tuesday = 2 from my csv but no luck.
Thanks in advance for nay help here
jonny
To convert a string containing a comma-delimited list into an array, use explode:
$array = explode(',', $days);
$this->item->tour_days is an array containing 1,2,3
Then why do you want to make another array out of it?
remove the $array = array($days); converstion.
Instead say $array = $days;
Try this:
$array = explode(",",$days);
This should work for you:
If $days is a string then use this:
$days = "1,2,3"; //$this->item->tour_days;
$array = explode(',', $days);
if(in_array(2, $array))
echo "Tuesday";
?>
If $days is already an array use this:
<?php
$days = array(1,2,3); //$this->item->tour_days;
if(in_array(2, $days))
echo "Tuesday";
?>
My array contain date $a1[0] day $a1[1] month $a1[2] year I want the result as year/day/month give me one solution.
Below is my code
<?php
$a1 = array("01","10","2012");
$result = implode("/",$a1);
print $result;
?>
This will print 01/10/2012 but I want this result 2012/01/10. How can I take the array manualy using implode function?
How can I take year first day second and month third?
You can't use implode alone for that matter, if you are having exactly this pattern (elements order are inverted) the use this code (YYYY/MM/DD):
$a1 = array("01", "10", "2012");
$result = implode("/", array_reverse($a1));
print $result;
Else use a regex (YYYY/MM/DD):
$a1 = array("01", "10", "2012");
$result = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3/$2/$1', implode("/", $a1));
print $result;
If you need the format of (YYYY/DD/MM) then use this one:
$a1 = array("01", "10", "2012");
$result = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3/$1/$2', implode("/", $a1));
print $result;
You can use array_reverse like this:
$result = implode("/", array_reverse($a1));
This will create a new array in the reverse order.
Use the mktime function. If you have a $timestamp, you can format is as you like with the date function:
$timestamp = mktime(0, 0, 0, $a1[1], $a1[0], $a1[2]);
echo date('y/m/d', $timestamp);
You can also use a loop (manual implode):
$a1 = array("01","10","2012");
$a1_length = sizeof($a1);
$date = '';
for ($i = $a1_length; $i > 0; $i--) {
$date .= $a1[$i] . '/';
}
$date = rtrim($date, '/');
echo $date;
<?php
$d1 = '1';
$d2 = '1';
$d3 = '1';
$date2 = ''.$d1.','.$d2.','.$d3.'';
I want to to put these values in an array, but when I assign a variable as array($date2) it just gets printed as 'Array'.
What is the problem?
*Updated, this is the code that is printing 'Array':
$date2 = array($date);
echo $date2;
Firstly, you cannot just echo array('data');.
To put data into an array such as that, you need to specify it in the following format:
$array = array($d1,$d2,$d3); //build array
print_r($array); //show array contents
echo $array[0] //echo first array element
You can also add data to an array as follows:
// add to array
$array[] = 'item 1';
$array[] = 'item 2';
// add to array
array_push($array,'item 3');
Additional reading: foreach & arrays
Try this:
$date2 = array($d1, $d2, $d3);
That will create an array with those values.
If you want to view the contents of the array, do this:
var_dump($date2);
If you want to show what's in an array you can use var_dump or print_r. You can't use echo to view an array (unless you do it like Briedis describes).
Putting in the array:
$array['key'] = "Value";
Printing out:
echo "Array: " . $array['key'];
$date2[] = array();
$date2[0] = 10;
$date2[1] = 20;
$date2[2] = 30;
$date2 = array();
array_push($date2,$d1);
array_push($date2,$d2);
array_push($date2,$d3);// or one in all $date2 = array($var1,$var,$var3);
print_r($date2);
for printing array use print_r
you should define $date2 like this:
$date2 = ($d1,$d2,$d3);
the way you have it now, $date2 is just a string of concatenated values
As you wrote using
print_r(array($date2));
will output Array ( [0] => 1,1,1 )
If you want add the three values separatly you should write this:
$date2 = array($d1, $d2, $d3);
Try the following code it will give you the expected output. There are three way print_r() , var_dump() and the ever green foreach(). you can use any of them.
<?php
$d1 = '1';
$d2 = '1';
$d3 = '1';
$date = ''.$d1.','.$d2.','.$d3.'';
$date2 = array($date);
echo "with print_r:";
print_r($date2);
echo "<hr/>";
echo "with var_dump:";
var_dump($date2);
echo "<hr/>";
echo "with foreach:<br/>";
foreach($date2 as $v)
echo "$v<br/>";
?>
max($caption);
gives me this:
Array
$caption is defined here:
$i = "0";
while ($i < $count) {
list($oldCaption, $year, $order) = explode("-", $galleryDirectory[$i]);
$caption[$year][$order] = str_replace("_", " ", "$oldCaption");
echo $year; //debug
echo "<br />";
$i++;
}
$year comes out to be
2008
2009
2009
so how do I get max($caption); to give me the value of 2009 rather than Array?
also i would put the whole code, but when i tried that it turned out messy. but I will try again, so you guys can see the whole pictures
Use array_keys():
$years = array_keys($caption);
$maxYear = max($years);
// or the one-liner:
$maxYear = max(array_keys($caption));
The reason why your code wasn't working is that you were comparing the values of the array $caption, which is an another array. max() compares int values, not keys nor arrays. By using array_keys() on $caption creates another array with all years as values.