2

I have these 2 functions in which I have to replace split with another php command:

function date_fr_mysql($date) {
    list($jour,$mois,$annee)=split("/",$date);
    $date = $annee."-".$mois."-".$jour;
    return $date;
} 
    
function date_mysql_fr($date) {
    list($annee,$mois,$jour)=split("-",$date);
    $date = $jour."/".$mois."/".$annee;  
    return $date;
} 

with which function I can replace it to get the same result?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bizboss
  • 7,792
  • 27
  • 109
  • 174
  • May I ask why you have to replace split? – gnab Mar 11 '11 at 16:31
  • 2
    Because Split has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. http://de3.php.net/manual/en/function.split.php – Bizboss Mar 11 '11 at 16:33
  • 1
    It looks like you're trying to translate date formats from US to French. If you're doing this a lot, you might want to look at the IntlDateFormatter class http://www.php.net/manual/en/class.intldateformatter.php – dnagirl Mar 11 '11 at 16:36

5 Answers5

7

You can use the explode function.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • In the OP's code (and in most uses of split, at least in my experience) this works great. In some cases though the functions aren't identical, as `split` supports regular expressions whereas `explode` does not. For ones that use regular expressions, `preg_split` is the go-to replacement. When you don't need this functionality, `explode` is faster. – cazort Aug 23 '21 at 21:22
5

The function explode is similar to split, except it does not regexes. Use preg_split if you need regex support.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
2

I have this example from PHP manual on split function over date:

<?php  
// Delimiters may be slash, dot, or hyphen                     `

    $date = "04/30/1973";  
    list($month, $day, $year) = split('[/.-]', $date);  
    echo "Month: $month; Day: $day; Year: $year";  
?> 

After some experimentation, the solution to avoid a deprecated warning and still have the same result is:

<?php  
    // Delimiters may be slash, dot, or hyphen  
    // test preg_split per /
     $valore2 = "2010/01/01";

    //list($month, $day, $year) = split('[/.-]', $valore2);
    list($year, $month, $day) = preg_split('[/|\.|-]', $valore2);
    echo "Month: $month; Day: $day; Year: $year\n";

    // test preg_split per -
    $valore2 = "2010-01-01";

    //list($month, $day, $year) = split('[/.-]', $valore2);
    list($year, $month, $day) = preg_split('[/|\.|-]', $valore2);
    echo "Month: $month; Day: $day; Year: $year\n";

    // test preg_split per .   
    $valore2 = "2010.01.01";

    //list($month, $day, $year) = split('[/.-]', $valore2);
    list($year, $month, $day) = preg_split('[/|\.|-]', $valore2);
    echo "Month: $month; Day: $day; Year: $year\n";
?>

Hope this helps.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • This seems like overkill! Check out the solutions using `explode`; the OP wasn't using the regular expressions capability of `split`, so that solution not only works, but it offers a performance gain. – cazort Aug 23 '21 at 21:24
1

Given it seems you're just changing - to /, how about

$date = str_replace('-', '/', $date);
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    the order of year, month and day is also being reversed, so it's a bit more than just str_replace() – Mark Baker Mar 11 '11 at 16:46
  • 1
    true 'nuff. Though since it's mysql_fr and fr_mysql, should be much easier to just format the dates in mysql to begin with, rather than doing processing in php. – Marc B Mar 11 '11 at 16:47
1
date ( 'Y-m-d', strtotime ( $your_date ) );
bensiu
  • 24,660
  • 56
  • 77
  • 117