-1

My task is to split a date fetching from DB and find the date after 8 years.

My tries are here -

Variables:

$doo = $info['s_doo']; // 2013-05-01
$validity = $info['s_validity']; // 8

Try 1

$str="+".$validity." year";
echo date("d / m / Y",strtotime($str,$doo)); // Does not work

Try 2

$str="+".($validity*12)." month";
echo date("d / m / Y",strtotime($str,$doo)); // Does not work

Try 3

$str="+".($validity*52)." week";
echo date("d / m / Y",strtotime($str,$doo)); // Works but Wrong result

Finally

list($y, $m, $d) = split('-',$doo); // Line 107

$str = ($y+$validity)."-".$m."-".$d;
echo date("d / m / Y",strtotime($str)); // 01 / 05 / 2021

The output stands:

Deprecated: Function split() is deprecated in D:\****\accinfo.php on line 107
01 / 05 / 2021

If it's generating a correct output why the error message is being displayed? I don't know what the Deprecated message for. I also tried using array instead of list and the split function like - split('-',$doo,10); split('-',$info['s_doo'],10); split('[-]',$doo); etc...

I need a good way to do the task. Thanks you.

Partharaj Deb
  • 864
  • 1
  • 8
  • 22
  • possible duplicate of [Why is `ereg` deprecated in PHP?](http://stackoverflow.com/questions/3078993/why-is-ereg-deprecated-in-php) or [Deprecated: Function split() is deprecated. How to rewrite this statement?](http://stackoverflow.com/questions/3453915/deprecated-function-split-is-deprecated-how-to-rewrite-this-statement) – hakre May 01 '13 at 16:07
  • And not to forget: [Function ereg() is deprecated](http://stackoverflow.com/q/4297501/367456) – hakre May 01 '13 at 16:38

4 Answers4

3

Use DateTime instead:

// input date (Y-m-d ?)
$doo = '2013-05-01';

// 8 years ?
$validity = new \DateInterval('P8Y');

// convert input date to DateTime object and add validity
$doo = \DateTime::createFromFormat('Y-m-d', $doo);
$doo->add($validity);   
print $doo->format('d/m/Y');
nice ass
  • 16,471
  • 7
  • 50
  • 89
2

Deprecated means that PHP language is going to stop support for the function in future. It will be removed from up coming versions of the language and so if you have a working code now, and you upgrade your PHP in the future your code will break because it is not available in this new version. Every deprecated function gets replaced by a new better function. Find that one and replace your function with the new one.

In order to inform users, PHP will show the deprecated message even if the function in question works currently in the present PHP version.

raidenace
  • 12,789
  • 1
  • 32
  • 35
  • @Raidance: Please leave useful links to the (most likely) duplicate information here on site and into the PHP manual in form of a comment if you want to help make this site a better place. Thank you! - P.S.: The web is based on a principle called hypertext: You do not need to write something again, you can just link. Very effective if you know how to use it! – hakre May 01 '13 at 16:08
  • 3
    @hakre *IF* I want to? Your comment has a really bad sting of patronising. Maybe you should tone it down a bit if *YOU* want to make this site a better place, my friend... – raidenace May 01 '13 at 16:09
  • Please take wordings in the internet lightly and mind the language gap. We are not all using english as first language and you seem to treat my writing as if it has been written with bad intentions (and no i'm not patronising you(?) [my spell-checker does not even know that word]). Rest assured it was not. – hakre May 01 '13 at 16:13
  • 1
    @hakre When someone says "_The web is based on a principle called hypertext.Very effective if you know how to use it!_" - especially in a tech site like SO, I think the intentions really cannot be mistaken. Neither the sayer nor the listener needs to have English as first language to get that. – raidenace May 01 '13 at 16:14
  • Why do you feel offended by that? Especially on a tech site this should leave you super cool. I don't understand. Is there a reason even? - And yes, hypertext is very effective if you know how to use it. Both in authoring hypertext documents as well as when reading and browsing those. – hakre May 01 '13 at 16:15
  • Forget it. I am not one for a lengthy sparring match. We are all trying to help here. Oh btw, these to-and-fros are what harms SO more than my not-so-hyperlinked answers. – raidenace May 01 '13 at 16:17
  • What does *"to-and-fros"* mean (and I didn't want to express you should provide hyperlink answers, I wrote more like it's worth a comment with a link)? – hakre May 01 '13 at 16:35
1

split() function is deprecated. You should use explode('-',$doo) which will split the string into an array.

Partharaj Deb
  • 864
  • 1
  • 8
  • 22
Ryan
  • 3,153
  • 2
  • 23
  • 35
1

As stated HERE split() is deprecated.

Use explode() instead like this:

list($y, $m, $d) = explode( '-' , $doo );
Mathlight
  • 6,436
  • 17
  • 62
  • 107