Sunday, May 1, 2011

PHP converting date format

Duplicate

Dear All,

I have a PHP page where i wil be displaying some data from Mysql db. I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002) I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'

Can any one guide me

Thanks in advance

From stackoverflow
  • You should be all set with this:

    echo date($dateFormat, strtotime($date1));
    echo date($dateFormat, strtotime($date2));
    
  • Use strtotime to convert the strings into a Unix timestamp, then use the date function to generate the correct output format.

    Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:

    $date1 = "28/04/2009";
    $date2 = "28-04-2009";
    
    function ukStrToTime($str) {
        return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
    }
    
    $date1 = date($dateFormat, ukStrToTime($date1));
    $date2 = date($dateFormat, ukStrToTime($date2));
    
    Shyju : When i pass "28/04/2009 " to the above mentioned method, i am getting "01/01/70"
    Jon Benedicto : I hadn't noticed that you were using UK time. I've updated my example.
    OIS : Its not "UK time", it is "not weird US date formats". Pretty much the rest of the world use dd/mm/yyyy see http://en.wikipedia.org/wiki/Calendar_date#dd.2Fmm.2Fyyyy_or_dd.mm.yyyy_.28day.2C_month.2C_year.29. ;)
    thomasrutter : I would have thought PHP would use the locale setting for its default date order, as in setlocale(). It'd be disappointing if it didn't.
    thomasrutter : Oooh apparently there is an alternative to strtotime when you want locale taken into account: http://au2.php.net/manual/en/function.strptime.php
    Jon Benedicto : The reason I didn't mention strptime is that it isn't available on Windows platforms.
  • If for some reason strtotime will not work for you, could always just replace the offending punctuation with str_replace.

    function dateFormat($date) {
    $newDate = str_replace(/, -, $date);
    echo $newDate;
    }
    
    echo dateFormat($date1);
    echo dateFormat($date2);
    

    I know this will make most folks cringe, but it may help you with formatting non-date strings in the future.

    Paolo Bergantino : str_replace expects strings, you're missing quotes.
  • You may want to look into the strptime function. This can convert any date from a string back into numeric values. Unlike strtotime, it can be adapted to different formats, including those from different locales, and its output is not a UNIX timestamp, so it's capable of parsing dates before 1970 and after 2037. It may be a little bit more work though because it returns an associative array though.

    Unfortunately it's not available on Windows systems either so it's not portable.

0 comments:

Post a Comment