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;
Related
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
This return something like array format
[{"DATE":"2021-06-11 00:00:00","point_no":"SRT-358"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-359"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-360"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-357"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-356"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-355"},{"DATE":"2021-06-09 00:00:00","point_no":"SRT-348"}]
I want to isolate DATE and point_no. For example
$year=['2015','2016','2017','2018','2019','2020'];
I tried $date['DOCDATE'] to isolate date it's not working. Is there any better way to fix this??
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
$date_array = array();
foreach($date as $dat){
array_push($date_array, \Carbon\Carbon::parse($dat["DATE"])->format('Y');
}
return $date_array;
You can use array_map function.
$extractYear = function($date) {
return substr($date["DATE"], 0, 4);
};
$years = array_map($extractYear, $date);
try this query -
$years= gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get()->map(function ($q) {
return [Carbon::parse($q->DATE)->format('Y')];
});
i have one variable like this :
$variable = "0904201600000123";
i want to make the first 8 digits become like this :
2016-04-09
have tried to use this code :
<?php
$name = "$variable;
$explode = explode("", $name);
echo $explode[0];
echo "-";
echo $explode[1];
echo "-";
echo $explode[2];
?>
but it does not work.
May you know where is the problem?
Thank you so much for your help.
You can access a variable as you can access an array, if you were to echo:
$string = "test";
echo $string[0];
It would echo t, and onwards.
You could concat the string and make a new variable like:
$v = "0904201600000123";
$date = $v[0].$v[1].'-'.$v[2].$v[3].'-'.$v[4].$v[5].$v[6].$v[7];
echo $date;
I tested it on my local machine and it worked
Note: If you want to use the method using the explode function as I saw you did earlier, you could use the str_split function which will instead make the result an array.
Substr() can break a string in parts.
$FirtsPart = Substr($variable, 0, 8);
$SecondPart = Substr($variable, 8, 8);
$FirstPart = strtotime('Y-m-d', $FirstPart);
$variable = $FirstPart . $SecondPart;
$variable = "0904201600000123";
preg_match("/([0-9]){2}([0-9]){2}([0-9]){4}/", $variable, $datematches);
$newdate = $datematches[3] . "-" . $datematches[2] . "-" . $datematches[1];
Chances are I've more than likely massively over complicated that. Not tested but should do the trick.
$variable = "0904201600000123";
Note: The date format of $variable didnt understand. If its not a standard format, considering it as a string and separate using substr
Option 1
$date = substr($variable, 0, 2).'-'.substr($variable, 2, 2).'-'.substr($variable, 4, 4);
echo $date;
Option 2
$dd = substr($variable, 0, 2);
$mm = substr($variable, 2, 2);
$yy = substr($variable, 4, 4);
$date = $dd.'-'.$mm.'-'.$yy;
echo $date;
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";
?>
<?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/>";
?>
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".