Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

PHP: Convert date format

Using PHP, how can I change this:
2011/03/16 08:00

to this:
03-16-2011
?
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Like so

$d = "2011/03/16 08:00";

echo date("m-d-Y", strtotime($d) );
Be sure to check dates such as 2011/03/05 and 2011/05/03.  strtotime() sometimes has a hard time distinguishing which format the incoming date is in.

If strtotime() doesn't get yours right, then you can pull out each piece of the date with substr().
SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or, if you are a fan of conciseness,

$olddate = '2011/03/16 08:00';
$newdate=substr($olddate,5,2)."-".substr($olddate,8,2)."-".$year=substr($olddate,0,4);