Change date format in php

I am creating a web page, since I need to change the date format from 01/22/2010 to 2010-01-22 I use the following function, but I get a warning like "Deprecated: the ereg () function has been canceled in c: \ wamp \ www \ testpage.php on line 33 ". In any case, to hide this error or is there any other way to change the date format? Please help me solve this problem. 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]; 
+6
php
source share
8 answers

You are using outdated features. Use preg_match instead. In addition, the preg_match call should be in the 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 } ?> 
+7
source share

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

Thus, you can easily change the date format as you wish using the many date options.

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

Outputs 2010-01-22

Documentation for functions used:

+11
source share

This should do it:

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

Or it could be without the date functions suggested by Gumbo:

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

I would recommend using date , though if you suspect that you need to change the format in the future. There is no need to use a regular expression for simple separation. In this case, Explode will be much faster.

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

About hiding errors; you should never hide notifications during development, as they help you create better code. Without this notification, you would happily use ereg , and your application could break badly when the server is upgraded to PHP 6. But you can control the number of errors displayed using error_reporting () . Turning error_reporting when your site is live can be a good idea.

By the way, start accepting some answers if you find them useful.

+3
source share

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))); 
+3
source share

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


Here is 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 will get:

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


More specifically, quoting the ereg documentation:

This feature has been DEPRECATED since PHP 5.3.0 and REDACTED as of PHP 6.0.0. Relying on this feature, very discouraged.

So, feel free to read the Perl-Compatible documentation - which are more powerful, faster, than POSIX.

+3
source share
 <?php list($day, $month, $year) = split('/', $_REQUEST['txtJoiningdate']); // 22/01/2010 $new_date = "$year-$month-$day"; // $new_date now equals 2010-01-22 ?> 
+1
source share
 $date = '01/24/2006'; echo date('Ym-d', strtotime($date)); // outputs 2006-01-24 
+1
source share
 if ( ! function_exists('changeDateFormat')) { function changeDateFormat($original) { //$original = '2015-08-10'; // 2015-08-10 to 10-08-2015 $exploded = explode("-", $original); $exploded = array_reverse($exploded); $newFormat = implode("-", $exploded); return $newFormat; } } 

0
source share

All Articles