1

I would like to remove the last certain characters of following array api variable 'min_date_created' like

$filter = array('min_date_created' => "$start_date");//query filter

Here the values of

'min_date_created'=Mon, 24 Sep 2012 00:53:26 +0000

So i want to remove the last 15 characters, so i expect the following format

'min_date_created'=Mon, 24 Sep 2012

So please any one help me how can i change this array variable 'min_date_created' in required format.

3 Answers 3

1

You can format using the date & strtotime function like below

https://www.php.net/strtotime

<?php 

  //format the date
 $min_date_created = date('D, d M Y', strtotime($start_date));

 $filter =  array('min_date_created'=>$min_date_created);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanking you. But here i don't want to change the value of variable $start_date .Just i want to removed the last 15 digits of array variable 'min_date_created' only. So i would like to expect some array functions like substr etc.
$start_date is different variable that will never change
1

Expanding Sundar's Answer , You can achieve simpler using an array_walk to do modify all the array elements in a single go.

<?php
$startdate="Mon, 24 Sep 2012 00:53:26 +0000"; // Usually you will be getting from a POST variable.
$filter = array('min_date_created' => $startdate);
array_walk($filter,'formatDT');
function formatDT(&$v,$k)
{
    $v=date('D, d M Y', strtotime($v));
}
print_r($filter);

OUTPUT :

Array
(
    [min_date_created] => Mon, 24 Sep 2012
)

Comments

0

You could use substr to remove the last 15 characters from $start_date before using it in the array.

$filter = array('min_date_created' => substr($start_date, 0, -15));

Output

array (
    'min_date_created' => 'Mon, 24 Sep 2012',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.