Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP: Convert date format

Posted on 2011-03-09
5
Medium Priority
?
381 Views
Last Modified: 2012-05-11
Using PHP, how can I change this:
2011/03/16 08:00

to this:
03-16-2011
?
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35086112
Like so

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

echo date("m-d-Y", strtotime($d) );
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 35086164
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().
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 800 total points
ID: 35086250
If there are problems as Yodercm suggests, another choice would be to convert it to exif format which strtotime definitely supports

EXIF       YY ":" MM ":" DD " " HH ":" II ":" SS       "2008:08:07 18:11:31"

http://uk.php.net/manual/en/datetime.formats.compound.php

$d = str_replace('/', ':', "2011/03/16 08:00");
0
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 1200 total points
ID: 35086315
Here is the simplest way:


$olddate = '2011/03/16 08:00';

$year=substr($olddate,0,4);
$month=substr($olddate,5,2);
$day=substr($olddate,8,2);

$newdate=$month."-".$day."-".$year;

echo $newdate;
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 35086330
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);
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question