change date format in php - php

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];

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:
strtotime
date
str_replace

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
}
?>

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)));

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.

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.

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

$date = '01/24/2006';
echo date('Y-m-d', strtotime($date)); // outputs 2006-01-24

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;
}
}

Related

Errors concerning depreciated eregi

This is the error i am receiving
Deprecated: Function eregi() is deprecated in /home/socia125/public_html/profile.php on
line231
This is my code
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match) {
// Find a match
if (eregi($Match, $agent)) {
break;
any help is appreciated
Well, as the manual quotes,
This function has been DEPRECATED as of PHP 5.3.0. Relying on this
feature is highly discouraged.
Use preg_match() instead. Be a bit careful though, because you cannot convert your search pattern 1:1 from eregi to preg_match().
Example to show the difference:
$t = "this is a test...";
if (preg_match("/test/i", $t)) echo "match!";
if (eregi('test', $t)) echo "match!";
There is a whole chapter in the php manual dedicated to the PCRE syntax.
But if you're just simply trying to find a string, use something like strstr or stristr, those are faster and easier to use.
Use preg_match instead of eregi.
But it seems like all it does is searching for a string, so you might be able to use stripos instead.
// Loop through the array of user agents and matching operating systems
foreach($OSList as $CurrOS=>$Match) {
// Find a match
if (stripos($Match, $agent) !== FALSE ) {
break;
I can't guarantee that it works as it should, since we don't see all of the code (especially contents and example values of $Match and $agent)
eregi() is deprecated as of 5.3.
You may ignore this as it is just a warning.
I would recommend to use preg_match() instead.
See the link here

php explode / split - co-existence in single script

I have php two servers with different versions of php,
and am having trouble with split statement which seems to be deprecated on new box.
I replaced with explode which is not known to old box.
$connect = explode(";", DB_CONNECT);
$connect = split(";", DB_CONNECT);
what statement(s) will make both servers happy?
Upgrading is not an option tonight.
A better option in the short term is to disable the warning until you're able to upgrade your PHP version.
See: http://php.net/manual/en/function.error-reporting.php
If explode doesnt exist, create it
if (!function_exists('explode')) {
function explode($str, $array) {
return split($str, $array);
}
}
Try preg_split() and preg_match_all(). The latter doesn't return an array, but may fill in an array pass in as third argument.
I have not tried this but hopefully it will work. Good luck.
function ultraExplode($del,$arr){
$ver=phpversion();
if ($ver>=5) return explode($del,$arr);
else return split($del,$arr);}

explode without variables [duplicate]

This question already exists:
Closed 11 years ago.
Possible Duplicate:
Access array element from function call in php
instead of doing this:
$s=explode('.','hello.world.!');
echo $s[0]; //hello
I want to do something like this:
echo explode('.','hello.world.!')[0];
But doesn't work in PHP, how to do it? Is it possible? Thank you.
As the oneliners say, you'll have to wait for array dereferencing to be supported. A common workaround nowadays is to define a helper function for that d($array,0).
In your case you probably shouldn't be using the stupid explode in the first place. There are more appropriate string functions in PHP. If you just want the first part:
echo strtok("hello.world.!", ".");
Currently not but you could write a function for this purpose:
function arrayGetValue($array, $key = 0) {
return $array[$key];
}
echo arrayGetValue(explode('.','hello.world.!'), 0);
It's not pretty; but you can make use of the PHP Array functions to perform this action:
$input = "one,two,three";
// Extract the first element.
var_dump(current(explode(",", $input)));
// Extract an arbitrary element (index is the second argument {2}).
var_dump(current(array_slice(explode(",", $input), 2, 1)));
The use of array_slice() is pretty foul as it will allocate a second array which is wasteful.
Not at the moment, but it WILL be possible in later versions of PHP.
This will be possible in PHP 5.4, but for now you'll have to use some alternative syntax.
For example:
list($first) = explode('.','hello.world.!');
echo $first;
While it is not possible, you technically can do this to fetch the 0 element:
echo array_shift(explode('.','hello.world.!'));
This will throw a notice if error reporting E_STRICT is on.:
Strict standards: Only variables should be passed by reference
nope, not possible. PHP doesn't work like javascript.
No, this is not possible. I had similar question before, but it's not

"Function split() is deprecated" in PHP?

$stringText = "[TEST-1] test task 1 Created: 06/Apr/11 Updated: 06/Apr/11";
$splitArray = split(" ",$stringText);
Deprecated: Function split() is deprecated in C:\wamp\www\RSS.php on line 27
Why this error happen ?
http://php.net/manual/en/function.split.php
From the manual
Warning This function has been
DEPRECATED as of PHP 5.3.0. Relying on
this feature is highly discouraged
Note:
As of PHP 5.3.0, the regex extension
is deprecated in favor of the PCRE
extension. Calling this function will
issue an E_DEPRECATED notice. See the
list of differences for help on
converting to PCRE.
I guess you're supposed to use the alternative preg_split(). Or if you're not using a regex, just use explode
split has been replaced with explode, see http://php.net/explode for more information. Works the same as split, but split is 'deprecated' basically means that is a old function that shouldn't be used anymore, and is not likely to be in later versions of php.
Use following explode function:
$command = explode(" ", $tag[1]);
This is the standard solution for this case.
Its Perfectly working.
Ahh, the docs says about it. And the docs also say which functions should be used instead of this:
preg_split
explode
str_split
Because the function has been deprecated? You can customize the error_reporting level to not log / display the depreciated errors. But it would be more prudent to just correct the issue (IE use explode instead for the simple split you are doing above.)
You can use this custom function for old codes:
if (!function_exists('split')) {
function split($pattern, $subject, $limit=-1, $flags=0){
return preg_split($pattern, $subject, $limit, $flags);
}
}

PHP: get array element

Why does this code not work?
echo explode("?", $_SERVER["REQUEST_URI"])[0];
It says syntax error, unexpected '['.
Oddly, this works:
$tmp = explode("?", $_SERVER["REQUEST_URI"]);
echo $tmp[0];
But I really want to avoid to create such a $tmp variable here.
How do I fix it?
After the helpful answers, some remaining questions: Is there any good reason of the design of the language to make this not possible? Or did the PHP implementors just not thought about this? Or was it for some reason difficult to make this possible?
Unlike Javascript, PHP can't address an array element after a function. You have to split it up into two statements or use array_slice().
This is only allowed in the development branch of PHP (it's a new feature called "array dereferencing"):
echo explode("?", $_SERVER["REQUEST_URI"])[0];
You can do this
list($noQs) = explode("?", $_SERVER["REQUEST_URI"]);
or use array_slice/temp variable, like stillstanding said. You shouldn't use array_shift, as it expects an argument passed by reference.
It's a (silly) limitation of the current PHP parser that your first example doesn't work. However, supposedly the next major version of PHP will fix this.
Take a look at this previous question.
Hoohaah's suggestion of using strstr() is quite nice. The following will return from the beginning of the string until the first ? (the third argument for strstr() is only available for PHP 5.3.0 onward):
echo strstr($_SERVER["REQUEST_URI"], "?", TRUE);
Or if you want to stick with explode, you can make use if list(). (The 2 indicates that at most 2 elements will be returned by explode).
list($url) = explode("?", $_SERVER["REQUEST_URI"], 2);
echo $url;
Finally, to use the natively available PHP URL parsing;
$info = parse_url($_SERVER["REQUEST_URI"]);
echo $info["scheme"] . "://" . $info["host"] . $info["path"];
EDIT:
echo current(explode("?", $_SERVER["REQUEST_URI"]));
I believe PHP 5.4 comes with this feature, at long last. Sadly, it will be a while before your average web host updates their PHP version. My Ubuntu VPS doesn't even have it in the default repos.

Categories