1

guys i am storing dates like these 07/01/2016, 07/02/2016, 07/03/2016, 07/04/2016 in db and when i fetch these dates from db using mysql_fetch_array() the result is the whole array now i want to get only the starting and ending date i.e 07/01/2016 and 07/04/2016...i tried using explode function but explode function breaks a string into an array...and i am unable to find a String To Array function in php...Kindly help me and tell me what should i do...

   $leave_dates=$_POST['dates'];
   $check_dates=explode(',', $leave_dates);
    foreach ($check_dates as $date)
{
    # code...
    $count_dates=count($date);
    //THIS GIVES THE COUNT OF DATES E.G 4
    NOW I WANT TO ACCESS THE FIRST AND THE LAST ELEMENT


}
3
  • 1
    Will this work for first $check_dates[0] and for last $check_dates[count($check_dates)-1] and also check by isset first Commented Jul 26, 2016 at 8:54
  • First = $check_dates[0]; $last= $check_dates[count($check_dates)-1]; Commented Jul 26, 2016 at 8:55
  • For first element: $check_dates[0] and for last: $check_dates[(sizeof($check_dates)-1)] Commented Jul 26, 2016 at 8:58

8 Answers 8

2

You could use reset and end

$firstDate = reset($check_dates);
$lastDate = end($check_dates);
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this

<?php
$row = array('07/01/2016','07/02/2016','07/03/2016','07/04/2016');
$total = count($row);
echo $row[0];
echo "<br>";
echo $row[$total-1];

output

07/01/2016
07/04/2016

Comments

0

you can also use array_shift and array_pop

$first_element = array_shift($check_dates);
$last_element = array_pop($check_dates);

Comments

0

try this

$firstDate = current($count_dates);
$lastDate = end($count_dates);

2 Comments

can you please expand you answer, and elaborate a bit on the solution?
current() and end() both are build pointer for the array in php. you can check the details php.net/manual/en/function.current.php php.net/manual/en/function.end.php
0

Best way is to work out on Queries.

select * from table_name
where id = (select id from tab1 order by col1 asc limit 1) or
id = (select id from tab1 order by col1 desc  limit 1);

Comments

0

try this ,

<?php 

    $check_dates=array('2016-5-11','2016-8-4','2016-4-9');
    $firstDate = current($check_dates);
    $lastDate = end($check_dates);

    print_r($firstDate);
    print_r($lastDate);

?>

Output will be :

 2016-5-11
 2016-4-9

Comments

0

here's the potential solution:

$leave_dates=$_POST['dates'];
$check_dates=explode(',', $leave_dates);
//To obtain the first element
$first_element=$check_dates[0];
//To obtain the last element
$last_element=$check_dates[count($check_dates)-1];

Comments

0

Since you store all the value in an array named $check_dates. You can access the first element of an array with index 0

$firstlement=$check_dates['0']; 

To access the last element of an array, you can use end function.

$lastement=end($check_dates);

1 Comment

it could be a comment, it's not a full-fledged answer. Please read the best practices: stackoverflow.com/help/how-to-answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.