Solved

PHP: Convert date format

Posted on 2011-03-09
5
371 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

737 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