Change date format in PHP

When coding in PHP & MySQL dealing with date is a huge headache if you are not prepared for it. As MySQL dates are in the format ‘yyyy-mm-dd’. However, we humans prefer date in the format of “dd/mm/yy” or “mm/dd/yy”! So when storing a date field to the database we need to change the date format to ymd, and after retrieving it from the database we need to change it to dmy or mdy.

To make life easier, it has been a while since I have used two functions for these changes. So here I share them, in case a newbie might need it.

function change2dmy($date) //input format: yyyy-m-d
{
$dtmp = explode(“-“,$date);
$dadate = mktime(0,0,0,$dtmp[1],$dtmp[2],$dtmp[0]);
return date(‘d/m/Y’,$dadate);
}
function change2ymd($date) //input format: d/m/yy or yyyy
{
$dtmp = explode(“/”,$date);
$dadate = mktime(0,0,0,$dtmp[1],$dtmp[0],$dtmp[2]);
return date(‘Y-m-d’,$dadate);
}

How to use it: it is a simple function.

<?php

$dmydate = ’24/12/08′
$ymddate = ‘2008-12-24’;

echo change2ymd($dmydate); //output: ‘2008-12-24′
echo change2dmy($ymddate); //output: ’24/12/2008’

?>

Advertisements

, ,

  1. #1 by Alfreeda Rodigues on January 20, 2009 - 7:22 pm

    Change the date function in PHP to get the date format as “February 14, 2006 @ 10:30pm”

  2. #2 by peds on July 8, 2009 - 8:14 pm

    but this function doesn’t return anything but a pile of errors.

  3. #3 by bougant on August 24, 2009 - 8:02 am

    it’s work…
    thanks alot…
    just need to do a little adjustment…

  4. #5 by Shahnewaj on March 11, 2010 - 9:31 am

    hello i am unable to run your code. it shows following error

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

  5. #6 by Yusuf Abdulla Shunan on March 11, 2010 - 5:59 pm

    I believe you are using it along with some of your codes, in order to help I will have to see your codes or you can check the below link and find a fix for yourself!

    http://www.parse-error-unexpected-t-string.com/

  6. #7 by KCD on July 19, 2010 - 1:24 pm

    test comments

  7. #8 by Vijer on October 14, 2010 - 11:10 pm

    The initial code doesn’t work because of the mktime line. Change mktime with strtotime.

    Here is the code that works.

    function change2ymd($date) {
    $dtmp = explode(“/”,$date);
    $dadate = date(“Y-m-d”, strtotime($dtmp[1].”-“.$dtmp[0].”-“.$dtmp[2]));
    return $dadate;
    }

  8. #9 by amad17 on March 29, 2011 - 12:23 pm

    thanx. your code really works 🙂
    kudos!

  9. #10 by septiadi on October 13, 2011 - 8:57 am

    try this one:
    echo date(“j F, Y”,strtotime(“2011-10-13”));

    will get output:
    13 October, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: