Search code examples
phpdatedate-conversion

Converting an 8-digit integer into dd/mm/yyyy


So i have this function in my PHP script that is supposed to take the date as an 8-digit integer such as 01042012 and convert it to 01/04/2012 for displaying.

Currently i'm trying to use php's date() function as follows:

$int = 01042012;

$date = date("d/m/Y", $int);

Instead of $date having the value 01/04/2012 it shows as 13/01/1970.

Is this because date() wants the date i pass to it to be in unix time? If so, would there perhaps be a more direct way to split the int after characters 2 and 4 and just insert the slashes, then recombine it? Like what explode() does, but i have no character to split with.


Solution

  • An easy way to do it;

    $int = 01042012;
    
    $day = substr($int,0,2);
    $month = substr($int,2,4);
    $year = substr($int,4);
    
    $date = $day.'/'.$month.'/'.$year;