tags:

views:

135

answers:

6

Hi friends, i develop a webpage , in that i need to change the date format from 22/01/2010 to 2010-01-22 i use the following function but i am getting a warning as "Deprecated : Function ereg() is depreceted in c:\wamp\www\testpage.php on line 33" . Is there anyway to hide that error or is there any other way to change the date format ? Please help me to solve this issue . Thanks in advance .

$datedue = $_REQUEST['txtJoiningdate'];
        $r = ereg ("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})", $datedue, $redgs);
        $billdate=$redgs[3]."-".$redgs[2]."-".$redgs[1];
+2  A: 

This should do it:

list($d, $m, $y) = explode('/', $datedue);
$billdate = date('Y-m-d', mktime(0,0,0,$m,$d,$y);

Or, this can be without the date functions as Gumbo suggested:

list($d, $m, $y) = explode('/', $datedue);
$billdate = "$y-$m-$d"; 

I'd recommend using the date though if you suspect you need to change the format in future. There is no need to use a regular expression for a simple splitting like that. Explode will be a lot faster in this case.

The ereg_ regular expression functions are deprecated as of PHP 5.3.0 and will be removed in PHP 6. For regular expressions, use the preg_ functions.

About hiding the error; you should never hide notices when developing as they help you build better code. Without that notice, you'd have happily used ereg and your application would have broken horribly when the server is updated to PHP 6. But, you can control the amount of shown errors with error_reporting(). Turning error_reporting off when your site goes live might be a good idea.

BTW, start accepting some answers if you find them helpful.

Tatu Ulmanen
Using `date` and `mktime` is not necessary. Simple string processing will suffice.
Gumbo
@Gumbo, using date (like I suggested too) offers more flexibility to change the date format on a future time without having to reorder or reprocess the string.
johnnyArt
+2  A: 

Use the PCRE functions preg_match or preg_replace instead:

$billdate = preg_replace('~([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})~', '$3-$2-$1', $datedue);

But you can also use a combination of explode, array_reverse and implode:

$billdate = implode('-', array_reverse(explode('/', $datedue)));
Gumbo
+1: That is neat :)
gameover
What’s the reason for the downvote?
Gumbo
+3  A: 

You are using deprecated functions. Use the preg_match instead. Also the call to preg_match should be in a if test.

<?php
$datedue = '22/01/2010';
if(preg_match('@([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})@', $datedue, $redgs)) {
    $billdate=$redgs[3]."-".$redgs[2]."-".$redgs[1];    
    echo $billdate; // prints  2010-01-22 
}
?>
codaddict
+2  A: 

With recent versions of PHP, POSIX regex functions are indeed deprecated -- you should stop using them, and use the preg_* functions instead.


Here's your code, rewritten to use preg_match :

preg_match("#([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})#", '22/01/2010', $redgs);
$billdate=$redgs[3]."-".$redgs[2]."-".$redgs[1];
var_dump($billdate);

And you'll get :

$ /usr/local/php-5.3/bin/php temp.php
string(10) "2010-01-22"


To be more precise, quoting the documentation of ereg :

This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

So, don't hesitate to read the documentation of Regular Expressions (Perl-Compatible) -- which are more powerfull, faster, ... than the POSIX ones.

Pascal MARTIN
+3  A: 

Why not use strtotime,date and str_replace functions native to php to do the trick in one simple line?

This way you could easily change the format of the date to whatever you want easily using the many options date offers.

echo date('Y-m-d',strtotime(str_replace("/",".","22/01/2010")));

Outputs 2010-01-22

Documentation for functions used:

johnnyArt
A: 
<?php
list($day, $month, $year) = split('/', $_REQUEST['txtJoiningdate']); // 22/01/2010
$new_date = "$year-$month-$day"; // $new_date now equals 2010-01-22
?>
Martin Bean