Solved

PHP: Convert date format

Posted on 2011-03-09
5
359 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
  • 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:yodercm
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:
yodercm 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:yodercm
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 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