Solved

PHP: Convert date format

Posted on 2011-03-09
5
375 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 200 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 300 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

717 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